Table of contents
1.
Introduction
2.
Calculate Sum of Natural Numbers in Python Using While Loop
2.1.
Example 1: Calculate the Sum of N Numbers Using While Loop
2.2.
Example 2: Calculate the Sum of N Numbers Using While Loop and User Input
3.
Method 2: Using Formula
3.1.
Example: Calculate Sum Using Formula
4.
Method 3: Using Recursion  
4.1.
Program to calculate the sum of first 'n' natural numbers using recursion  
4.2.
Example
4.3.
Why should we use Recursion?  
5.
Comparison of Methods
6.
Frequently Asked Questions
6.1.
Can I use a for loop instead of a while loop? 
6.2.
Why should I use the formula method instead of loops? 
6.3.
What happens if I enter a negative number? 
7.
Conclusion
Last Updated: Aug 28, 2025
Easy

Python Program to Find the Sum of Natural Numbers

Introduction

The sum of natural numbers is a fundamental concept in mathematics and programming. In  Python, we can calculate this sum using loops, recursion, or mathematical formulas. 

Python Program to Find the Sum of Natural Numbers

This program will take an integer input from the user and compute the sum of all natural numbers up to that number. It is commonly used in algorithms, data processing, and problem-solving scenarios.

Calculate Sum of Natural Numbers in Python Using While Loop

One of the simplest ways to find the sum of natural numbers is by using a while loop. In this method, we initialize a sum variable to 0 and keep adding numbers from 1 to N in a loop.

Example 1: Calculate the Sum of N Numbers Using While Loop

# Program to calculate the sum of N natural numbers using while loop
n = 10  # Define the value of N
sum_n = 0  # Initialize sum variable
counter = 1  # Initialize counter
while counter <= n:
    sum_n += counter  # Add counter to sum
    counter += 1  # Increment counter
print("The sum of the first", n, "natural numbers is:", sum_n)
You can also try this code with Online Python Compiler
Run Code

 

Output:

The sum of the first 10 natural numbers is: 55


Explanation:

  • We start with sum_n = 0 and counter = 1.
     
  • We add counter to sum_n in each iteration.
     
  • The loop stops when counter exceeds n.
     
  • The final sum is displayed as output.

Example 2: Calculate the Sum of N Numbers Using While Loop and User Input

Instead of defining n manually, we can take input from the user.

# Program to calculate sum of N natural numbers with user input

n = int(input("Enter a positive integer: "))  # Take user input
sum_n = 0  # Initialize sum variable
counter = 1  # Initialize counter

while counter <= n:
    sum_n += counter  # Add counter to sum
    counter += 1  # Increment counter

print("The sum of the first", n, "natural numbers is:", sum_n)
You can also try this code with Online Python Compiler
Run Code

 

Example Output:

Enter a positive integer: 5
The sum of the first 5 natural numbers is: 15


Explanation:

  • The user provides input for n.
     
  • The program calculates the sum using a while loop.
     
  • The final sum is printed as output.

Method 2: Using Formula

A faster and more efficient way to calculate the sum of n natural numbers is by using the formula:

Sum = n * (n + 1) / 2


This formula eliminates the need for loops and reduces execution time significantly.

Example: Calculate Sum Using Formula

# Program to calculate the sum of N natural numbers using formula

n = int(input("Enter a positive integer: "))  # Take user input
sum_n = n * (n + 1) // 2  # Use formula to calculate sum

print("The sum of the first", n, "natural numbers is:", sum_n)
You can also try this code with Online Python Compiler
Run Code

 

Example Output:

Enter a positive integer: 7
The sum of the first 7 natural numbers is: 28

 

Explanation:

  • The formula n * (n + 1) / 2 is used to find the sum directly.
     
  • The division // ensures the result is an integer.
     
  • The output is displayed instantly without using a loop.

Method 3: Using Recursion  

Recursion is another interesting way to solve the problem of finding the sum of the first 'n' natural numbers. In simple terms, recursion is a technique where a function calls itself to solve smaller instances of the same problem.  

This is how recursion works for this problem:  

1. We define a function that takes 'n' as an argument.  
 

2. If 'n' is 1, the function returns 1 (since the sum of the first 1 natural number is 1). This is called the base case.  
 

3. For any other value of 'n', the function calls itself with 'n - 1' & adds 'n' to the result. This is called the recursive case.  

 

Let’s see the complete code:  

Program to calculate the sum of first 'n' natural numbers using recursion  

Step 1: Define the recursive function  

def sum_of_natural_numbers(n):  
     Base case: If n is 1, return 1  
    if n == 1:  
        return 1  
     Recursive case: Add n to the sum of the first (n-1) natural numbers  
    else:  
        return n + sum_of_natural_numbers(n - 1)  


Step 2: Take input from the user  

n = int(input("Enter a positive integer: "))  


Step 3: Call the function & store the result  

result = sum_of_natural_numbers(n)  


Step 4: Print the result  

print("The sum of the first", n, "natural numbers is:", result)  


In this Code:  

1. Function Definition: The function `sum_of_natural_numbers(n)` is defined to calculate the sum recursively.  
 

  • Base Case: When `n == 1`, the function returns 1. This stops the recursion.  
     
  • Recursive Case: For `n > 1`, the function calls itself with `n - 1` & adds `n` to the result.  
     

2. Input: The user is prompted to enter a positive integer 'n'.  
 

3. Function Call: The function is called with the input value 'n', & the result is stored in the variable `result`.  
 

4. Output: The calculated sum is printed.  

Example

If the user enters `n = 5`, the function will work as follows:  

  • `sum_of_natural_numbers(5)` calls `5 + sum_of_natural_numbers(4)`  
     
  • `sum_of_natural_numbers(4)` calls `4 + sum_of_natural_numbers(3)`  
     
  • `sum_of_natural_numbers(3)` calls `3 + sum_of_natural_numbers(2)`  
     
  • `sum_of_natural_numbers(2)` calls `2 + sum_of_natural_numbers(1)`  
     
  • `sum_of_natural_numbers(1)` returns 1 (base case)  

The final calculation will be:  

5 + 4 + 3 + 2 + 1 = 15  


The output will be:  

The sum of the first 5 natural numbers is: 15  

Why should we use Recursion?  

  • Conceptual Understanding: Recursion helps you understand how problems can be broken down into smaller, similar subproblems.  
     
  • Elegance: The code looks clean & concise.  


However, Recursion may not be the best choice for very large values of 'n' because it can lead to stack overflow errors due to excessive function calls.  

Comparison of Methods

MethodTime ComplexitySpace Complexity
While LoopO(n)O(1)
FormulaO(1)O(1)

 

Outcomes:

  • The while loop method is simple and easy to understand but takes more time for large values of n.
     
  • The formula-based approach is more efficient and recommended for large numbers.

Frequently Asked Questions

Can I use a for loop instead of a while loop? 

Yes, a for loop can also be used to iterate from 1 to n and sum the numbers.

Why should I use the formula method instead of loops? 

The formula method is faster because it executes in constant time O(1), while loops take linear time O(n).

What happens if I enter a negative number? 

The code should include validation to ensure that n is a positive integer to avoid incorrect results.

Conclusion

In this article, we explored how to write a Python program to find the sum of natural numbers using both iterative and recursive methods. This program helps in understanding loops, recursion, and mathematical computations in  Python. Mastering this concept improves problem-solving skills and enhances efficiency in algorithm development.

Recommended Readings:

Live masterclass