Python example to find Sum and Average of Natural Numbers

n = int(input("Enter number"))
sum = 0
# loop from 1 to n
for num in range(1, n + 1, 1):
    sum = sum + num
print("Sum of first ", n, "numbers is: ", sum)
average = sum / n
print("Average of ", n, "numbers is: ", average)

OUTPUT:

Enter number 10
Sum of first  10 numbers is:  55
Average of  10 numbers is:  5.5

Leave a Comment