A Program to Calculate the Sum of Natural Numbers
In this example, we are giving a program to calculate the sum of n natural numbers.
Program to calculate the sum of n natural number:
# Calculate the sum of n natural numbernum = int(input('Enter a number: '))if num < 0:print("Enter a positive number")else:sum = 0while(num > 0):sum += numnum -= 1print("The sum is", sum)
Output:
Enter a number: 17 The sum is 153