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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *