Friday, October 24, 2025
HomeLanguagesPython Program to Find Sum of N Natural Numbers

Python Program to Find Sum of N Natural Numbers

Sum of n natural numbers in python; In this tutorial, you will learn how do you write a Python program to find the sum of the first n natural number using while loop, for loop, and recursion function.

The sum of natural numbers formula is used to find the sum of the natural numbers up to n terms. i.e., 1 + 2 + 3 + 4 + 5 + …. up to n terms. To derive the formula, we need to use the sum of the arithmetic progression formula, because the natural numbers are arranged in an arithmetic sequence. With 1 as the first term, 1 as the common difference, and up to n terms, we use the sum of an AP = n/2(2+(n-1)). Solving this, you get the sum of natural numbers formula = [n(n+1)]/2.

Sum of Natural Numbers Formula = [n(n+1)]/2 .

Python Programs to Find/Calculate Sum Of n Natural Numbers

Let’s use the following algorithm to write a program to find sum of n natural numbers in python:

  • Python Program to Calculate Sum of N Natural Numbers using While Loop
  • Python Program to find Sum of N Natural Numbers using For Loop
  • Python Program to calculate Sum of N Natural Numbers using Recursion Function

Python Program to Calculate Sum of N Natural Numbers using While Loop

Follow the below steps and write a program to find the sum of first n natural numbers using while loop in python:

  • Take input number from the user
  • Iterate while loop and calculate sum of n natural number
  • As well as store value in variable
  • Print sum of n natural number
# Python Program to find Sum of N Natural Numbers
 
number = int(input("Please Enter any Number: "))

total = 0
value = 1

while (value <= number):
    total = total + value
    value = value + 1

print("The Sum of Natural Numbers from 1 to {0} =  {1}".format(number, total))

Output

Please Enter any Number: 5
The Sum of Natural Numbers from 1 to 5 =  15

Python Program to find Sum of N Natural Numbers using For Loop

Follow the below steps and write a program to find the sum of first n natural numbers using for loop in python:

  • Take input number from the user
  • Iterate for loop and calculate sum of n natural number
  • As well as store value in variable
  • Print sum of n natural number
# Python Program to find Sum of N Natural Numbers
 
number = int(input("Please Enter any Number: "))
total = 0

for value in range(1, number + 1):
    total = total + value

print("The Sum of Natural Numbers from 1 to {0} =  {1}".format(number, total))

Output

Please Enter any Number: 10
The Sum of Natural Numbers from 1 to 10 =  55

Python Program to calculate Sum of N Natural Numbers using Recursion Functions

Follow the below steps and write a program to find the sum of first n natural numbers using function in python:

  • Take input number from the user
  • Define a function, which is calcuate sum of n natural number
  • As well as store value in variable
  • Print sum of n natural number
# Python Program to find Sum of N Natural Numbers
 
def sum_of_n_natural_numbers(num):
    if(num == 0):
        return num
    else:
        return (num + sum_of_n_natural_numbers(num - 1))
    
number = int(input("Please Enter any Number: "))

total_value = sum_of_n_natural_numbers(number)

print("Sum of Natural Numbers from 1 to {0} =  {1}".format(number, total_value))

Output

Please Enter any Number: 10
The Sum of Natural Numbers from 1 to 10 =  55

Recommended Python Programs

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS