Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Python Assignment Answers Simple

The document contains a series of Python programming assignments with solutions. Each question includes a specific task, such as checking for palindromes, calculating the sum of digits, generating Fibonacci numbers, and implementing recursive functions for various mathematical operations. The solutions are provided as code snippets that demonstrate the required functionality.

Uploaded by

Aryan Ingole
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python Assignment Answers Simple

The document contains a series of Python programming assignments with solutions. Each question includes a specific task, such as checking for palindromes, calculating the sum of digits, generating Fibonacci numbers, and implementing recursive functions for various mathematical operations. The solutions are provided as code snippets that demonstrate the required functionality.

Uploaded by

Aryan Ingole
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Programming - I Assignment Answers (Simplified)

Q1. Write a Python program to check whether a number is a palindrome.


num = input("Enter a number: ")
if num == num[::-1]:
print("Palindrome")
else:
print("Not a palindrome")

Q2. Implement a program to find the sum of digits of a given number.


num = input("Enter a number: ")
sum_digits = sum(int(digit) for digit in num)
print("Sum of digits:", sum_digits)

Q3. Write a program to generate and print the first n Fibonacci numbers using a loop.
n = int(input("Enter n: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

Q4. Implement a program to check if a number is a strong number.


import math
num = int(input("Enter a number: "))
temp = num
sum_fact = sum(math.factorial(int(digit)) for digit in str(num))
print("Strong Number" if sum_fact == temp else "Not a Strong Number")

Q5. Write a Python program to find the Least Common Multiple (LCM) of two numbers.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
gcd = 1
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0:
gcd = i
lcm = (a * b) // gcd
print("LCM:", lcm)

Q6. Write a recursive function to compute the sum of the first n natural numbers.
def sum_n(n):
if n == 1:
return 1
return n + sum_n(n - 1)
n = int(input("Enter n: "))
print("Sum:", sum_n(n))

Q7. Implement a recursive function to find the factorial of a number.


def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
n = int(input("Enter n: "))
print("Factorial:", factorial(n))

Q8. Write a recursive function to count the number of digits in a given number.
def count_digits(n):
if n < 10:
return 1
return 1 + count_digits(n // 10)
n = int(input("Enter number: "))
print("Number of digits:", count_digits(n))

Q9. Implement a recursive function to find the sum of elements in a list.


def sum_list(lst):
if len(lst) == 1:
return lst[0]
return lst[0] + sum_list(lst[1:])
lst = [int(x) for x in input("Enter numbers: ").split()]
print("Sum:", sum_list(lst))

Q10. Write a recursive function to generate the binary representation of a given decimal
number.
def to_binary(n):
if n <= 1:
return str(n)
return to_binary(n // 2) + str(n % 2)
n = int(input("Enter a number: "))
print("Binary:", to_binary(n))

You might also like