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 number
num = int(input('Enter a number: '))
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output:
Enter a number: 17 The sum is 153