Python Program to Find the Sum of Natural Numbers

To understand this example, you should have the knowledge of the following Python programming topics:


In the program below, we've used an if...else statement in combination with a while loop to calculate the sum of natural numbers up to num.

Source Code

# Sum of natural numbers up to num

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)

Output

The sum is 136

Note: To test the program for a different number, change the value of num.

Initially, the sum is initialized to 0. And, the number is stored in variable num.

Then, we used the while loop to iterate until num becomes zero. In each iteration of the loop, we have added the num to sum and the value of num is decreased by 1.


We could have solved the above problem without using a loop by using the following formula.

n*(n+1)/2

For example, if n = 16, the sum would be (16*17)/2 = 136.

Your turn: Modify the above program to find the sum of natural numbers using the formula below.


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to find the sum of first N natural numbers.

  • Hint: The formula for the sum of the first N natural numbers is N*(N+1)/2.
  • For example, for input 5, the outout should be 15.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges