Python Programming Assignment 2
Question 1:
Python program to print the sum of digits
Answer: total_sum=0
while True:
num=input("Enter a number: ").strip()
if num.isdigit():
total_sum+=sum(int(digit)for digit in num)
else:
print("Please enter a valid number.")
if input("Do you want to continue? (yes/no): ").strip().lower()!='yes':
break
print("Total sum of all digits entered:", total_sum)
Question 2:
Python program to check prime number
Answer:
num = int(input('Enter a number: '))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print('Not Prime')
break
else:
print('Prime')
else:
print('Not Prime')
Question 3:
Python program to print the table of a number
Answer:
num = int(input('Enter a number: '))
for i in range(1, 11):
print(f'{num} x {i} = {num * i}')
Question 4:
Python program to print the factorial of a number
Answer:
import math
num = int(input('Enter a number: '))
print(f'Factorial: {math.factorial(num)}')
Question 5:
Python program to check Armstrong number
Answer:
num = int(input('Enter a number: '))
power = len(str(num))
sum_of_powers = sum(int(digit) ** power for digit in str(num))
if num == sum_of_powers:
print('Armstrong Number')
else:
print('Not an Armstrong Number')
Question 6:
Python program to check palindrome number
Answer:
num = input('Enter a number: ')
if num == num[::-1]:
print('Palindrome')
else:
print('Not a Palindrome')
Question 7:
Python program to print Fibonacci series without recursion
Answer:
n = int(input("Enter how many Fibonacci numbers you want: "))
a, b = 0, 1
print("Fibonacci series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
Question 8:
Python program to reverse a given number
Answer:
num = input('Enter a number: ')
print(f'Reversed Number: {num[::-1]}')
Question 9:
Python program to reverse a given string
Answer:
string = input('Enter a string: ')
print(f'Reversed String: {string[::-1]}')
Question 10:
Python program to swap two numbers with and without using a third variable
# With third variable
a = int(input('Enter the first number (a): '))
b = int(input('Enter the second number (b): '))
temp = a
a = b
b = temp
print(f'After Swap (with third variable): a = {a}, b = {b}')
# Without third variable
a = int(input('Enter the first number (a): '))
b = int(input('Enter the second number (b): '))
a, b = a + b, a - b
a = a - b
print(f'After Swap (without third variable): a = {a}, b = {b}')
Question 11:
Python program to find the area of a triangle
Answer:
base = float(input('Enter base of the triangle: '))
height = float(input('Enter height of the triangle: '))
area = 0.5 * base * height
print(f'Area of triangle: {area}')
Question 12:
Python program to find if the given year is a leap year or not
Answer:
year = int(input('Enter a year: '))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print('Leap Year')
else:
print('Not a Leap Year')
Question 13:
Python program to check whether the given number is prime or not
Answer:
num = int(input('Enter a number: '))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print('Not Prime')
break
else:
print('Prime')
else:
print('Not Prime')
Question 14:
Python program to find the greatest number among three given numbers
Answer:
a = int(input('Enter the first number (a): '))
b = int(input('Enter the second number (b): '))
c = int(input('Enter the third number (c): '))
print(f'Greatest number: {max(a, b, c)}')
Question 15:
Python program to print the numbers 1-10 in reverse order
Answer:
for i in range(10, 0, -1):
print(i, end=' ')
Question 16:
Python program to find the sum of the elements of a list
Answer:
lst=[]
while True:
a=int(input('Enter the first number: '))
b=int(input('Enter the second number: '))
lst.append(a)
lst.append(b)
if input('Do you want to enter more numbers? (yes/no): ').strip().lower()!='yes':
break
print(f'Sum of elements: {sum(lst)}')
Question 17a
Python program to print a star triangle (right-angled triangle):
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
print("*" * i)
Question 17b
Python program to print a reversed star triangle:
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
print("*" * i)
Question 18
Python program to convert Celsius to Fahrenheit
Answer:
celsius = float(input('Enter temperature in Celsius: '))
fahrenheit = (celsius * 9/5) + 32
print(f'Temperature in Fahrenheit: {fahrenheit}')
Question 19
Python Program to Print Sum of First Ten Natural Numbers
Answer:
print(sum(range(1, 11)))
Question 20
Python Program to Find LCM
Answer:
def lcm(a, b):
greater = max(a, b)
while True:
if greater % a == 0 and greater % b == 0:
return greater
greater += 1
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
print(f'LCM: {lcm(num1, num2)}')
Question 21
Python Program to Find HCF
Answer:
import math
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
print(f'HCF: {math.gcd(num1, num2)}')
Question 22
Python Program to Calculate the Power of a Number
Answer:
base = float(input('Enter base: '))
exponent = float(input('Enter exponent: '))
print(f'Result: {pow(base, exponent)}')
Question 23
Python program to print first ten natural numbers
Answer:
for i in range(1, 11):
print(i, end=' ')
Question 24
Python Program to check whether given string is a palindrome or not
Answer:
string = input('Enter a string: ')
if string == string[::-1]:
print('Palindrome')
else:
print('Not a palindrome')
Question 25: Python Program to remove duplicates from a list
Answer:
lst = []
while True:
element = int(input('Enter a number: '))
lst.append(element)
continue_choice = input('Do you want to enter more numbers? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
print("List without duplicates:", list(set(lst)))
za
Question 26: Python program to copy all elements of one array into another array
Answer:
arr1 = []
while True:
element = int(input('Enter an element for the array: '))
arr1.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
arr2 = arr1.copy()
print("Copied Array:", arr2)
Question 27: Python program to find the frequency of each element in the list
Answer:
from collections import Counter
lst = []
while True:
element = int(input('Enter an element: '))
lst.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
freq = Counter(lst)
print("Element frequencies:", dict(freq))
Question 28: Python program to print the duplicate elements of an array
Answer:
lst = []
while True:
element = int(input('Enter an element: '))
lst.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
duplicates = [item for item in set(lst) if lst.count(item) > 1]
print("Duplicate elements:", duplicates)
Question 29: Python program to print the elements of a list present on even positions
Answer:
lst = []
while True:
element = int(input('Enter an element: '))
lst.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
print("Elements at even positions:", [lst[i] for i in range(1, len(lst), 2)])
Question 30: Python program to print the elements of a list present on odd positions
Answer:
lst = []
while True:
element = int(input('Enter an element: '))
lst.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
print("Elements at odd positions:", [lst[i] for i in range(0, len(lst), 2)])
Question 31: Python program to print the sum of all elements in a list
Answer:
lst = []
while True:
element = int(input('Enter an element: '))
lst.append(element)
continue_choice = input('Do you want to enter more elements? (yes/no): ').strip().lower()
if continue_choice != 'yes':
break
print("Sum of elements:", sum(lst))
32. Python program to add two matrices
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Y = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
result = [[X[i][j] + Y[i][j] for j in range(len(X[0]))] for i in range(len(X))]
print("Resultant matrix after addition:")
for row in result:
print(row)
33. Python program to multiply two matrices
X = [[1, 2], [3, 4]]
Y = [[5, 6], [7, 8]]
result = [[sum(a * b for a, b in zip(X_row, Y_col)) for Y_col in zip(*Y)] for X_row in X]
print("Resultant matrix after multiplication:")
for row in result:
print(row)
34. Python program to transpose a matrix
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
transpose = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print("Transposed matrix:")
for row in transpose:
print(row)