Find the Sum of Natural Numbers Python

PROGRAM TO FIND THE SUM OF N NATURAL NUMBERS


num = 16

if num < 0:
   print("Enter a positive number")
else:
   sum = 0
   # use while loop to iterate until zero
   while(num > 0):
       sum += num
       num -= 1
   print("The sum is", sum)print("The sum is", (num*(num+1))/2)


OUTPUT
The sum is 136
The sum is 136

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java