Solutions to Programming Problems
1. Python program to display Fibonacci numbers up to n.
Solution:
def fibonacci(n):
a, b = 0, 1
while a < n:
print(a, end=" ")
a, b = b, a + b
2. Python program that computes the sum of all the elements of a list.
Solution:
def sum_of_list(lst):
return sum(lst)
3. Python program that accepts a string and prints its reverse.
Solution:
def reverse_string(s):
return s[::-1]
4. Python program to find the factorial of a number using recursion.
Solution:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
5. Python program to check if a number is prime.
Solution:
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
6. Python program to find the sum of digits of a given number.
Solution:
def sum_of_digits(num):
total = 0
while num > 0:
total += num % 10
num //= 10
return total
7. Python program to merge two lists into a single list.
Solution:
def merge_lists(list1, list2):
return list1 + list2
8. Python program to remove duplicates from a list.
Solution:
def remove_duplicates(lst):
return list(set(lst))
9. Python program to sort a list of numbers.
Solution:
def sort_list(lst):
[Link]()
return lst
10. Python program to count the occurrences of a specific element in a list.
Solution:
def count_element(lst, element):
return [Link](element)
11. Python program to check whether a given number is Armstrong number.
Solution:
def is_armstrong(num):
num_str = str(num)
num_digits = len(num_str)
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
return sum_of_powers == num
12. Python program to generate a random number between two given numbers.
Solution:
import random
def generate_random_number(start, end):
return [Link](start, end)
13. Python program to find the largest number in a list.
Solution:
def largest_number(lst):
return max(lst)
14. Python program to check if a string is a palindrome.
Solution:
def is_palindrome(s):
return s == s[::-1]
15. Python program to find the sum of even numbers in a given range.
Solution:
def sum_of_evens(start, end):
return sum(i for i in range(start, end + 1) if i % 2 == 0)
16. Python program to find the sum of digits of a number using recursion.
Solution:
def sum_of_digits(n):
if n == 0:
return 0
return n % 10 + sum_of_digits(n // 10)
17. Python program to check if a number is a perfect square.
Solution:
import math
def is_perfect_square(n):
return [Link](n) ** 2 == n
18. Python program to check if a number is an automorphic number.
Solution:
def is_automorphic(n):
square = n * n
return str(square).endswith(str(n))
19. Python program to find the factorial of a number using iteration.
Solution:
def factorial_iterative(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
20. Python program to find the common elements in two lists.
Solution:
def common_elements(list1, list2):
return list(set(list1) & set(list2))
21. Python program to find the length of a string without using the built-in len() function.
Solution:
def string_length(s):
count = 0
for char in s:
count += 1
return count
22. Python program to remove vowels from a string.
Solution:
def remove_vowels(s):
vowels = "aeiouAEIOU"
return ''.join([char for char in s if char not in vowels])
23. Python program to check if a number is a Harshad number.
Solution:
def is_harshad_number(n):
digit_sum = sum(int(digit) for digit in str(n))
return n % digit_sum == 0
24. Python program to calculate the power of a number using recursion.
Solution:
def power(base, exp):
if exp == 0:
return 1
return base * power(base, exp - 1)
25. Python program to find the area of a triangle given its three sides using Heron's formula.
Solution:
import math
def area_of_triangle(a, b, c):
s = (a + b + c) / 2
area = [Link](s * (s - a) * (s - b) * (s - c))
return area