Python Assignment Solutions 2
Python Assignment Solutions 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:
Question 4:
import math
num = int(input('Enter a number: '))
print(f'Factorial: {math.factorial(num)}')
Question 5:
Question 7:
print("Fibonacci series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b
Question 8:
Question 9:
Python program to swap two numbers with and without using a third variable
Question 11:
Question 12:
Question 13:
Question 14:
Python program to find the greatest number among three given numbers
Answer:
Question 15:
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
Question 17b
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
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')
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:
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))
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
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
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
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