codescracker


python

Python Program to Find Sum of Natural Numbers



« Previous Program Next Program »


Find Sum of Natural Numbers in Python

To find sum of natural numbers in python, you have to ask from user to enter the total number of term upto which he/she want to find the sum of all natural numbers upto the desired term and then print the summation result as output as shown in the program given below.

Python Programming Code to Find Sum of Natural Numbers

Following python program ask from user to enter the limit to find the sum of all natural numbers upto that limit:

# Python Program - Find Sum of Natural Numbers
		
while True:
	print("Enter '0' for exit.")
	num = int(input("Upto which number ? "))
	if num == 0:
		break
	elif num < 1:
		print("Please, enter a positive number...")
	else:
		sum = 0
		while num > 0:
			sum += num
			num -= 1
		print("Sum = ", sum)

Here is the sample run of the above Python program to illustrate how to find and print sum of natural numbers:

python find sum of natural numbers

« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test