;

Python Program to Find the Sum of Natural Numbers


Tutorialsrack 22/04/2020 Python

In this Python program, we will learn how to find the sum of the natural numbers. Natural numbers are the positive integers or non-negative integers that start from 1 and end at infinity, such as 1,2,3,4,5,6,7,8,9,10,……,∞. 

Here is the code of the program to find the sum of the natural numbers. 

Python Program to Find the Sum of Natural Numbers
# Python Program to Find the Sum of Natural Numbers

# Sum of natural numbers up to num

# Take the Input From the User
num = int(input("Enter the Number: "))
holdNum = num

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 of First",holdNum,"Natural Number is: ", sum)
Output

Enter the Number: 10

The Sum of First 10 Natural Number is:  55


Related Posts



Comments

Recent Posts
Tags