Sem-2 Practical Practice Question2

Q.2 Write a python program to print sum of N numbers using UDF.

SOLUTION.

# Define a user-defined function to calculate sum
def sum_of_n_numbers(n):
    total = 0
    for i in range(n):
        num = float(input(f"Enter number {i + 1}: "))
        total += num
    return total

# Main program
n = int(input("How many numbers do you want to sum? "))
result = sum_of_n_numbers(n)
print(f"\nThe sum of {n} numbers is: {result}")

Leave a Reply

Your email address will not be published. Required fields are marked *