0% found this document useful (0 votes)
43 views26 pages

Python Assignment 8

Uploaded by

rraushanraj362
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
43 views26 pages

Python Assignment 8

Uploaded by

rraushanraj362
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 26

ASSIGNMENT PYTHON

NAME-raushan kumar
1. SE
COURCE-BCA `1st` YEAR
ROLL NO-23SCSE1040408
1. Python program to convert the temperature in degree centigrade to
Fahrenheit.

PROGRAM:-

c = input(" enter temprature in centigrade:")


f= (9*(int(c))/5)+32
print("temprature in fahrenheit is:",f)

OUTPUT:-

enter temprature in centrigrade: 45


temprature in fahrenheit is: 113.0

2. Python program to find the area of a triangle whose sides are given.

PROGRAM:-

a= int(input("enter the value of first side"))


b= int(input("enter the value of second side"))
c= int(input("enter the value of third side"))

s= (a+b+c)/2
area= (s*(s-a)*(s-b)*(s-c))**.5
print("The area of triangle",area)

OUTPUT:-

enter the value of first side: -4


enter the value of second side: -6
enter the value of third side: -9
The area of triangle 9.56229574945264

3. Python program to find the circumference and are a of a circle with a given
radius.

PROGRAM:-

a= int(input("the value of radius of circle"))


perimeter= 2*3.14*a
print("circumference of ci rcle",perimeter)

area= 3.14*a*a

print("the area of circle",area)

OUTPUT: -

the value of radius of circle 9


circumference of ci rcle 56.52
the area of circle 254.34

4. Python program to find the geometric mean of n numbers


PROGRAM:-

multiply_values = 8*16*22*12*41
n=5
geometric_mean = (multiply_values)**(1/n)
print ('The Geometric Mean is: ' + str(geometric_mean))

OUTPUT: -

the Geometric Mean is: 16.916852032061655

5. Python program to display the sum of n numbers using a list

PROGRAM:-

total = 0
ele = 0

list1 = [11, 5, 17, 18, 23]

while(ele < len(list1)):


total = total + list1[ele] ele
+= 1

print("Sum of all elements in given list: ", total)


OUTPUT:-

Sum of all elements in given list: 74

6. Python program to find out the average of a set of integers

PROGRAM:-

def digitAverage(n):
digiSum, digiLen = 0, 0

while n:
digiLen += 1
digiSum += n % 10 n
= n//10

return (digiSum/digiLen)

print(digitAverage(642)) print(digitAverage(3504))

OUTPUT: -

4.0
3.0

7. program to check whether the given number is


even or not
PROGRAM:-

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


if (num % 2) == 0:

print("{0} is Even".format(num)) else:

print("{0} is Odd".format(num))

OUTPUT:-

Enter a number: 4

4 is Even

8. Python program to find the product of a set of real numbers

PROGRAM:-

# Function to find the product of a set of real numbers

def find_product(numbers):

product = 1 for

num in numbers:

product *= num

return product

# Example set of real numbers numbers

= [2.5, 3.0, 1.5, 4.0]

# Calculate the product result =

find_product(numbers)

# Print the result


print("Product of the numbers:", result)

PROGRAM:-

OUTPUT:- 9. Python program to check whether the given


integer is a multiple of 5

Product of the numbers: 45.0


# Input: Get an integer from the user

num = int(input("Enter an integer:"))

# Check if the integer is a multiple of 5

if num % 5 == 0:

print(f"{num} is a multiple of 5")

else:

print(f"{num} is not a multiple of 5")

OUTPUT:-

Enter an integer: 25
Is a multiple of 5

10. Python program to check whether the given integer is a multiple of both
5 and 7

PROGRAM:-

num = int(input("Enter an integer: "))


if num % 5 == 0 and num % 7 == 0:

print(f"{num} is a multiple of both 5 and 7")

else: print(f"{num} is not a multiple of both 5

and 7")

OUTPUT: -

Enter an integer:35
Is a multiple of both 5 and 7

11.python program to find the average of 10 numbers using while loop

PROGRAM:-

sum = 0

for _ in range(10): n=

float(input('Enter a number: '))

sum = sum + n

average = sum/10

print(f'The average of these numbers is: {average}')

OUTPUT:-

Enter a number: 10

Enter a number: 12

Enter a number: 122

Enter a number: 12.34


Enter a number: 43.2

Enter a number: 12.333

Enter a number: 77.10

Enter a number: 98

Enter a number: 89.22

Enter a number: 90.1

The average of these numbers is: 56.6293

12. Python program to display the given integer in a reverse manner

PROGRAM:-

number = int(input("Enter the integer number: "))


revs_number = 0

while (number > 0):


# Logic remainder = number % 10
revs_number = (revs_number * 10) + remainder
number = number // 10

# Display the result print("The reverse number is :


{}".format(revs_number))

OUTPUT:-

Enter the integer number: 12345


The reverse number is: 54321

13. Python program to find the sum of the digits of an integer using a
while loop

PROGRAM:-
def
getSum(n):
sum = 0 while
(n != 0):

sum = sum + (n % 10)


n = n//10

return sum

n = 12345
print(getSum(n))

OUTPUT:-

15

14. Python program to display all the multiples of 3 within the range
10 to 50
PROGRAM:-
start = 10 end = 50 print("Multiples of 3 in the

range from 10 to 50:") for num in range(start,

end + 1): if num % 3 == 0: print(num)

OUTPUT:-

Multiples of 3 in the range from 10 to 50:

12

15

18

21

24

27

30

33
36

39

42

45

48

15. Python program to display all integers within the range 100-200
whose sum of digits is an even number

PROGRAM:-

start = 100 end = 200

def sum_of_digits(n):

return sum(int(digit) for digit in str(n)) print("Integers within the

range 100-200 with an even sum of digits:") for num in range(start,

end + 1): digit_sum = sum_of_digits(num) if digit_sum % 2 == 0:

print(num, end=" ")

OUTPUT:-

Integers within the range 100-200 with an even sum of digits:


101 103 105 107 109 110 112 114 116 118 121 123 125 127 129 130 132 134 136 138 141 143
145 147 149 150 152 154 156 158 161 163 165 167 169 170 172 174 176 178 181 183 185 187
189 190 192 194 196 198 200

16. Python program to check whether the given integer is a prime


number or not

PROGRAM:-
def is_prime(num): if num <= 1:

return False if num <= 3:

return True if num % 2 == 0 or

num % 3 == 0:

return False

i=

while i * i <= num: if num % i == 0

or num % (i + 2) == 0:

return False

i += 6

return True

num = int(input("Enter an integer: "))

if is_prime(num):

print(f"{num} is a prime number") else:

print(f"{num} is not a prime number")

OUTPUT:-
Enter an integer: 2

2 is a prime number

OUTPUT: -

Enter an integer: 1

1 is not a prime number


17. Python program to find the roots of a quadratic equation

PROGRAM:-

print("Equation: ax^2 + bx + c ")

a=int(input("Enter a: ")) b=int(input("Enter

b: ")) c=int(input("Enter c: "))

d=b**2-4*a*c d1=d**0.5

if(d<0):

print("The roots are imaginary. ") else:

r1=(-b+d1)/2*a r2=(-b-d1)/2*a

print("The first root: ",round(r1,2))

print("The second root: ",round(r2,2))

OUTPUT:-
Equation: ax^2 + bx + c

Enter a: 14

Enter b: 8

Enter c: 9

The roots are imaginary.

18. Python program to find the factorial of a number using recursion

PROGRAM:-
def factorial(n):

if n == 0:

return 1 # 0! is defined as 1

else:
return n * factorial(n - 1) num =

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

if num < 0:

print("Factorial is not defined for negative numbers.") else:

result = factorial(num) print(f"The

factorial of {num} is {result}")

OUTPUT:-
Enter a non-negative integer: 3

The factorial of 3 is 6

19. Python program to find the odd numbers in an array

PROGRAM:-

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print("Odd numbers in the array:")

for num in numbers: if num % 2 !=

0: print(num)

OUTPUT:-
Odd numbers in the array:

9
20. Python program to implement linear search

PROGRAM:-
def linear_search(arr, target):

for i in range(len(arr)):

if arr[i] == target:

return i

return -1 arr = [5, 10, 15, 20, 25, 30] target =

int(input("Enter the element to search for: ")) index =

linear_search(arr, target) if index != -1:

print(f"{target} found at index {index}") else:

print(f"{target} not found in the list.")

OUTPUT:-

Enter the element to search for: 20


20 found at index 3

21. Python program to implement binary search

PROGRAM:-
def binary_search(arr, target):

left, right = 0, len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid # Target found, return the index


elif arr[mid] < target:

left = mid + 1 # Adjust the left boundary

else:

right = mid - 1 # Adjust the right boundary

return -1 # Target not found, return -1

# Input: Create a sorted array (list) of elements (you can modify this as needed) arr

= [10, 20, 30, 40, 50, 60, 70]

# Input: Get the target value from the user target =

int(input("Enter the value you want to search for: "))

# Perform binary search result =

binary_search(arr, target)

if result != -1:

print(f"{target} is found at index {result}") else:

print(f"{target} is not in the array")

OUTPUT:-

Enter the value you want to search for: 50

50 is found at index 4

23. Python program to find the largest number in a list without using
built-in functions PROGRAM:-
# Input: Create a list of numbers (you can modify this as needed) numbers

= [10, 30, 45, 67, 23, 56, 98, 12]


# Initialize a variable to store the largest number largest

= numbers[0]

# Iterate through the list to find the largest number

for number in numbers: if number > largest:

largest = number

# Print the largest number print("The largest

number in the list is:", largest)

OUTPUT:-

The largest number in the list is: 98

24. Python program to insert a number to any position in a list


PROGRAM:-
# Input: Create a list of numbers (you can modify this as needed) numbers

= [10, 20, 30, 40, 50]

# Input: Get the number to insert and the position from the user

value = int(input("Enter the number to insert: ")) position =

int(input("Enter the position to insert the number: "))

# Check if the position is within the valid range if

0 <= position <= len(numbers):

numbers.insert(position, value) print(f"{value}

inserted at position {position}. Updated list:

{numbers}") else:

print("Invalid position. The position should be between 0 and", len(numbers))


OUTPUT:-
Enter the number to insert: 35

Enter the position to insert the number: 3

35 inserted at position 3. Updated list: [10, 20, 30, 35, 40, 50]

25. Python program to check whether a string is palindrome or not

PROGRAM:-

def is_palindrome(s):

s = s.lower() # Convert the string to lowercase to ignore capitalization s = ''.join(e for

e in s if e.isalnum()) # Remove spaces and non-alphanumeric characters return s ==

s[::-1] # Check if the string is equal to its reverse

# Input: Get a string from the user input_string

= input("Enter a string: ")

if is_palindrome(input_string):

print(f"'{input_string}' is a palindrome.") else:

print(f"'{input_string}' is not a palindrome.")

OUTPUT 1:-

Enter a string: kayak

'kayak' is a palindrome.
OUTPUT2:-
Enter a string: SWATI

'SWATI' is not a palindrome.

26. Python program to implement matrix addition PROGRAM:-


def matrix_addition(A, B):

result = [[0 for i in range(len(A[0]))] for j in range(len(A))]

for i in range(len(A)): for j in range(len(A[0])):

result[i][j] = A[i][j] + B[i][j]

return result

def matrix_subtraction(A, B):

result = [[0 for i in range(len(A[0]))] for j in range(len(A))]

for i in range(len(A)): for j in range(len(A[0])):

result[i][j] = A[i][j] - B[i][j]

return result

def matrix_multiplication(A, B):

result = [[0 for i in range(len(B[0]))] for j in range(len(A))]

for i in range(len(A)):

for j in range(len(B[0])):

for k in range(len(B)):

result[i][j] += A[i][k] * B[k][j]

return result # Example matrices

matrix_A = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

matrix_B = [[9, 8, 7],


[6, 5, 4],

[3, 2, 1]]

# Perform matrix operations

print("Matrix A:") for row in

matrix_A: print(row)

print("Matrix B:") for row in

matrix_B:

print(row)

print("Matrix Addition:") result_addition =

matrix_addition(matrix_A, matrix_B) for row in

result_addition:

print(row)

print("Matrix Subtraction:") result_subtraction =

matrix_subtraction(matrix_A, matrix_B) for row in

result_subtraction:

print(row)

print("Matrix Multiplication:") result_multiplication =

matrix_multiplication(matrix_A, matrix_B) for row in

result_multiplication:

print(row)

OUTPUT:-

Matrix A:
[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

Matrix B:

[9, 8, 7]

[6, 5, 4]

[3, 2, 1]

Matrix Addition:

[10, 10, 10]

[10, 10, 10]

[10, 10, 10]

Matrix Subtraction:

[-8, -6, -4]

[-2, 0, 2]

[4, 6, 8]

Matrix Multiplication:

[30, 24, 18]

[84, 69, 54]

[138, 114, 90]

27. Python program tofind diagonal elements of a matrix PROGRAM:-


# Function to find diagonal elements of a matrix

def find_diagonal_elements(matrix):

diagonal_elements = [] # Check if the matrix

is square if len(matrix) == len(matrix[0]):

for i in range(len(matrix)):

diagonal_elements.append(matrix[i][i])

return diagonal_elements

else:
return "The matrix is not square, so it doesn't have diagonal elements."

# Example matrix

matrix = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

# Call the function to find diagonal elements

diagonal_elements = find_diagonal_elements(matrix)

# Print the diagonal elements print("Diagonal elements of the

matrix are:", diagonal_elements)

OUTPUT:-
Diagonal elements of the matrix are: [1, 5, 9]

27. Python program to print all the items in a dictionary PROGRAM:-


# Sample dictionary my_dict

={

"name": "John",

"age": 30,

"city": "New York"

# Printing all items in the dictionary for

key, value in my_dict.items():

print(f"{key}: {value}")
OUTPUT:-
name: John

age: 30 city:

New York

28. Python program to draw a circle of squares using Turtle


PROGRAM:-
import turtle

# Set up the turtle screen window

= turtle.Screen()

window.title("Circle inside

Square")

# Create a turtle object pen

= turtle.Turtle()

# Function to draw a square def

draw_square(side_length):

for _ in range(4):

pen.forward(side_length) pen.right(90)

# Function to draw a circle def

draw_circle(radius):

pen.circle(radius)
# Set the initial position of the turtle

pen.penup() pen.goto(-50, -50)

pen.pendown()

# Draw the square square_length

= 100 pen.color("blue")

draw_square(square_length)

# Draw the circle inside the square

circle_radius = square_length / 2 * (2 ** 0.5)

pen.penup() pen.goto(0, -circle_radius)

pen.pendown()

pen.color("red") draw_circle(circle_radius)

# Close the turtle graphics window on click

window.exitonclick()

29. Python program to implement matrix multiplication PROGRAM:-

# Function to perform matrix multiplication def

multiply_matrices(matrix1, matrix2): # Initialize result matrix with

zeros result = [[0 for _ in range(len(matrix2[0]))] for _ in

range(len(matrix1))]

# Perform matrix multiplication

for i in range(len(matrix1)):

for j in range(len(matrix2[0])):

for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]

return result

# Example matrices to be multiplied matrix1

= [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

matrix2 = [[9, 8, 7],

[6, 5, 4],

[3, 2, 1]]

# Call the function and print the result result_matrix

= multiply_matrices(matrix1, matrix2) print("Result

of matrix multiplication:") for row in result_matrix:

print(row)

OUTPUT:-
Result of matrix multiplication:

[30, 24, 18]

[84, 69, 54]

[138, 114, 90]

30. Python program to check leap year PROGRAM:-


def is_leap_year(year):

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

return True

else:

return False
# Get user input for the year year

= int(input("Enter a year: "))

# Check if the entered year is a leap year or not if

is_leap_year(year):

print(year, "is a leap year.")

else: print(year, "is not a leap

year.")

OUTPUT:-
Enter a year: 2021

2021 is not a leap year.

COMPLETE

You might also like