0% found this document useful (0 votes)
11 views3 pages

Programming Solutions

Uploaded by

Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Programming Solutions

Uploaded by

Aditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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):

lst.sort()

return lst

10. Python program to count the occurrences of a specific element in a list.

Solution:

def count_element(lst, element):

return lst.count(element)

... (and so on for all solutions).

You might also like