Write a program in python to find the sum of first 5 natural numbers

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.

Python Program to Find the Sum of Natural Numbers

Natural numbers:

As the name specifies, a natural number is the number that occurs commonly and obviously in the nature. It is a whole, non-negative number.

Some mathematicians think that a natural number must contain 0 and some don't believe this theory. So, a list of natural number can be defined as:

See this example:

This example shows the sum of the first 100 positive numbers (0-100)

Output:

Write a program in python to find the sum of first 5 natural numbers


Write a program in python to find the sum of first 5 natural numbers
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

Write a program in python to find the sum of first 5 natural numbers
Write a program in python to find the sum of first 5 natural numbers
Write a program in python to find the sum of first 5 natural numbers





This is a Python Program to find the sum of first N Natural Numbers.

Problem Description

The program takes in the the number of terms and finds the sum of first N Natural Numbers.

Problem Solution

1. Take in the number of natural numbers to find the sum of and store it in a separate variable.
2. Initialize the sum variable to 0.
3. Use a while loop to find the sum of natural numbers and decrement the number for each iteration.
4. The numbers are added to the sum variable and this continues until the the value of the number is greater than 0.
5. Then the sum of first N natural numbers is printed.
6. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of first N Natural Numbers. The program output is also shown below.

n=int(input("Enter a number: "))
sum1 = 0
while(n > 0):
    sum1=sum1+n
    n=n-1
print("The sum of first n natural numbers is",sum1)

Program Explanation

1. User must enter the number of natural numbers to find the sum of.
2. The sum variable is initialized to 0.
3. The while loop is used to find the sum of natural numbers and the number is decremented for each iteration.
4. The numbers are added to the sum variable and this continues till the value of the variable is greater than 0.
5. When the value of the variable becomes lesser than 0, the total sum of N natural numbers is printed.

Runtime Test Cases

 
Case 1:
Enter a number: 18
The sum of first n natural numbers is 171
 
Case 2:
Enter a number: 167
The sum of first n natural numbers is 14028

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Write a program in python to find the sum of first 5 natural numbers

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

How do you write a program to sum the first n natural numbers in Python?

27 comments on “Python Program to Find the Sum of First N Natural Numbers”.
Fahad. def sum(n): result=0. for i in range(n+1): ... .
Kiran. num=int(input(“enter a number”)) sum=(num*(num+1)/2) print(“the sum of “,num, “natural numbers is : {}”.format(sum)) ... .
pavanicherla62. Simple program for this quetion is: n= int(input(“”)) i=1..

How do you find the sum of 5 numbers in Python?

“program for sum of five numbers in python” Code Answer's.
a = int(input("Enter first number:")).
b = int(input("Enter second number:")).
sum = a+b..
print(sum).

How do you find the sum of a natural number in Python?

See this example:.
num = int(input("Enter a number: ")).
if num < 0:.
print("Enter a positive number").
sum = 0..
# use while loop to iterate un till zero..
while(num > 0):.
sum += num..

What is the sum of first five natural numbers?

So, the set of natural numbers will be { 1, 2, 3, 4, 5, 6, …………. }. So, now from the set of natural numbers. First five numbers will be 1, 2, 3, 4 and 5. So, the sum of these five numbers is 1 + 2 + 3 + 4 + 5 = 15.