0% found this document useful (0 votes)
0 views18 pages

pythonfile.suryansh

This document is a practical file submitted by Suryansh Sharma for the BCA 2nd semester at the University of Jammu, focusing on problem-solving using Python. It includes acknowledgments, a certificate of completion, an index of programs, and detailed code for various Python programs addressing mathematical and logical problems. The programs cover tasks such as addition, checking for even/odd, prime numbers, and matrix operations.

Uploaded by

singhomkar737
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views18 pages

pythonfile.suryansh

This document is a practical file submitted by Suryansh Sharma for the BCA 2nd semester at the University of Jammu, focusing on problem-solving using Python. It includes acknowledgments, a certificate of completion, an index of programs, and detailed code for various Python programs addressing mathematical and logical problems. The programs cover tasks such as addition, checking for even/odd, prime numbers, and matrix operations.

Uploaded by

singhomkar737
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIVERSITY OF JAMMU

BHADERWAH CAMPUS

BACHELORS IN COMPUTER APPLICATIONS


(BCA-2ND SEMESTER 2025-28)

PRACTICAL FILE
PROBLEM SOLVING USING PYTHON

SUBMITTED BY: SUBMITTED TO:


Suryansh Sharma Ms. Janvi Ma’am
213040010

1
ACKNOWLEDGEMENT

I would like to express my gratitude to all those people who helped me to


complete this practical file. I want to thank Department of computer
science and IT for providing me all the tools and resources for the
completion of my practical file.

I have further more to thank our respected teacher Ms. Janvi Ma’am who
encouraged me to move ahead and complete my work in time.

At last, I want to thank my classmates and friends for their help, support,
interest and valuable hints. I want to thank once again all the above
mentioned persons for their time and help.

Thank you.

Suryansh Sharma
BCA 2nd semester
213040010

2
CERTIFICATE

This is to certify that Suryansh Sharma student of BCA 2nd semester


bearing university Roll no. 213040010 has completed that required
number of practicals of Problem solving using Python under the
guidance of class teacher Ms. Janvi Ma’am from the Department of
Computer science and IT, Bhaderwah campus, University of Jammu.

3
Signature. Signature.
(I/C course coordinate) (Practical I/C )

4
S no.
INDEX Programs Page no.
1 WAP to add two numbers. 05
2 WAP to check if the number is even or odd. 05
3 WAP to check enter number is prime or not. 06
4 WAP to find the sum of first n natural numbers. 06
5 WAP to print Fibonacci series up to n terms. 07
6 WAP to print factorial of a number. 07
7 WAP to check enter number is an Armstrong number or 08
not.
8 WAP to print a full pyramid. 08
9 WAP to display all the factors of a number. 09
10 WAP to calculate the average of all elements in an array. 09
11 WAP to find the determinent of 2x2 matrix. 10-11
12 WAP to read two matrices and find their sum. 12-13
13 WAP to display prime numbers between two intervals 14
using function and for loop.
14 WAP to find common Array Elements between two 15
Arrays

*****

5
Program 1 WAP to add two numbers.
# Input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Adding the numbers


sum = num1 + num2

# Display the result


print("The sum is:", sum)

OUTPUT

Program 2:- WAP to check if the number is even or odd.

# Input from user


num = int(input("Enter a number: "))

# Check if even or odd


if num % 2 == 0:
print(num, "is an Even number.")
else:
print(num, "is an Odd number.")

OUTPUT

6
7
Program 3:- WAP to check enter number is prime or not.
#to enter the number from user
num = int(input("Enter a number: "))

# Prime numbers are greater than 1


if num > 1:
# Check for factors
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(num, "is not a prime number.")
break
else:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
OUTPUT

Program 4:- WAP to find the sum of first n natural numbers.


# Input from user
n = int(input("Enter a positive integer: "))

# Check if the input is valid


if n > 0:
# Using the formula: sum = n * (n + 1) / 2
total = n * (n + 1) // 2
print("The sum of first", n, "natural numbers is:", total)
else:
print("Please enter a positive integer.")

OUTPUT

8
Program 5:- WAP to print Fibonacci series up to n terms.

# Input from user OUTPUT


n = int(input("Enter the number of terms: "))

# First two terms


a, b = 0, 1
count = 0

if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print("Fibonacci sequence:")
print(a)
else:
print("Fibonacci sequence:")
while count < n:
print(a, end=' ')
a, b = b, a + b
count += 1

Program 6:- WAP to print factorial of a number.


num = int(input("Enter a non-negative integer: "))

if num < 0:
print("Factorial is not defined for negative numbers.")
else:
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is:", factorial)

OUTPUT

9
Program 7:- WAP to check enter number is an Armstrong number or
not.

# Input from user OUTPUT


num = int(input("Enter a number: "))

# Store the number of digits


order = len(str(num))

# Initialize sum
sum = 0
temp = num

# Calculate the sum of the digits raised to the power 'order'


while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

# Check if it is an Armstrong number


if sum == num:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")

Program 8:- WAP to print a full pyramid.

# Input from user


rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):


# Print leading spaces
print(" " * (rows - i), end="")

# Print stars with spaces


print("* " * I)

10
OUTPUT

11
Program 9:- WAP to display all the factors of a number.

# Input from the user


num = int(input("Enter a number: "))

print(f"Factors of {num} are:")

# Loop through 1 to the number itself


for i in range(1, num + 1):
if num % i == 0:
print(i)

OUTPUT

Program 10:- WAP to calculate the average of all elements in an array.


# Input array from user
arr = list(map(float, input("Enter elements of the array separated by space: ").split()))

# Calculate sum and average


total = sum(arr)
average = total / len(arr)

print(f"The average of the array elements is: {average}")

OUTPUT

12
Program 11:- WAP to find the determinent of 2x2 matrix.
import numpy as np

# Function to input a matrix


def input_matrix(rows, cols):

print(f"Enter the elements row-wise for a {rows}x{cols} matrix:")


matrix = []

for i in range(rows):
row = list(map(float, input(f"Row {i+1}: ").split()))
if len(row) != cols:
print("Incorrect number of elements, please try again.")
return None
[Link](row)
return [Link](matrix)

# Input first matrix


print("Matrix 1:")
matrix1 = input_matrix(2, 2)

# Input second matrix


print("\nMatrix 2:")
matrix2 = input_matrix(2, 2)

# Calculate and display determinants


if matrix1 is not None and matrix2 is not None:
det1 = [Link](matrix1)
det2 = [Link](matrix2)

print(f"\nDeterminant of Matrix 1: {round(det1, 2)}")


print(f"Determinant of Matrix 2: {round(det2, 2)}")
else:
print("Failed to read matrices properly.

OUTPUT

13
14
Program 12:- WAP to read two matrices and find their sum.

# Function to read a matrix


def read_matrix(rows, cols):
print(f"Enter the elements row-wise for a {rows}x{cols} matrix:")
matrix = []
for i in range(rows):
row = list(map(int, input(f"Row {i+1}: ").split()))
if len(row) != cols:
print("Invalid number of elements. Please try again.")
return None
[Link](row)
return matrix

# Function to add two matrices


def add_matrices(mat1, mat2):
result = []
for i in range(len(mat1)):
row = []
for j in range(len(mat1[0])):
[Link](mat1[i][j] + mat2[i][j])
[Link](row)
return result

# Input matrix dimensions


rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

# Read both matrices


print("Matrix 1:")
matrix1 = read_matrix(rows, cols)

print("\nMatrix 2:")
matrix2 = read_matrix(rows, cols)
# Check and calculate sum
if matrix1 and matrix2:
sum_matrix = add_matrices(matrix1, matrix2)
print("\nSum of the matrices:")
for row in sum_matrix:
print(row)
else:
print("Matrix input was invalid.")

15
OUTPUT

16
Program 13:- WAP to display prime numbers between two intervals
using function and for loop.

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

def display_primes(start, end):


print(f"Prime numbers between {start} and {end} are:")
for num in range(start, end + 1):
if is_prime(num):
print(num, end=' ')
print() # for newline

# Example usage
start_interval = int(input("Enter start of interval: "))
end_interval = int(input("Enter end of interval: "))
display_primes(start_interval, end_interval)

OUTPUT

17
Program 14:- WAP to Find Common Array Elements between Two
Arrays.

# Define the arrays


array1 = [1, 2, 3, 4, 5]
array2 = [4, 5, 6, 7, 8]

# Initialize an empty list to store common elements


common_elements = []

# Use a for loop to find common elements


for element in array1:
if element in array2 and element not in common_elements:
common_elements.append(element)

# Display the result


print("Common elements:", common_elements)

OUTPUT

********

18

You might also like