Program 3
Q3. Write a menu driven Python Program to find Factorial and sum of list
of numbers using function.
Source Code:
# Function to calculate the factorial of a number
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Function to calculate the sum of a list of numbers
def sum_of_list(numbers):
total = 0
for number in numbers:
total += number
return total
# Test the functions
number = 5
numbers_list = [1, 2, 3, 4, 5]
print(f"Factorial of {number} is {factorial(number)}")
print(f"Sum of the list {numbers_list} is {sum_of_list(numbers_list)}")
output:
Factorial of 5 is 120
Sum of the list [1, 2, 3, 4, 5] is 15