Python program to print sum of N natural numbers
Find the Sum of N Natural Numbers in Python
Given an integer input number, the objective is to sum all the numbers that lay from 1 to the integer input number and print the sum. In order to do so we usually use iteration to sum all the numbers until the input variable number.
Example Input : number = 5 Output : 15 Explanation : 1 + 2 + 3 + 4 + 5 = 15
Find the Sum of N Natural Numbers
Given an integer value the objective of the code is to sum up all the numbers until the input integer number. To do so we usually use iteration, we iterate through the numbers until the input number is reached while appending the number to the sum variable. Here are some methods to solve the above mentioned problem
- Method 1: Using for Loop
- Method 2: Using Formula
- Method 3: Using Recursion
We’ll discuss the above methods in detail in the sections below.
Method 1: Using for Loop
In this method we’ll use for loops to iterate through all the numbers until the integer input “number” is reached.
Working
For an integer input “number” we do the following
- Initialize the required variables.
- Using for loop iterate from 0 to number+1.
- Meanwhile append the number to sum variable.
- Print the sum variable.
Let’s implement the above logic in Python code.
Python Code
number,sum = 6,0 for i in range(number+1): sum+=i print(sum)
Output
21
Method 2: Using the Formula
In this method we’ll use the formula for finding the sum of N integers in a series from series and sequences i.e sum = number * ( number + 1 ) / 2 to calculate the sum until the given integer input.
Working
For an integer input “number” we perform the following steps
- Initialize sum variable as sum = (number * ( number + 1 ) /2 ).
- Print the sum variable.
Let’s implement the above logic in Python Language.
Python Code
number = 6 sum = int((number * (number + 1))/2) print(sum)
Output
21
Method 3: Using Recursion
In this method we’ll use recursion to recursively iterate through the number while appending them to the sum variable until the number is reach which here act as the base case. To learn more about recursion, check out Recursion in Python.
Working
For an integer input “number” we do
- Initialize the required variables.
- Define a recursive function with base case as number ==0.
- Set the step recursive call as number + recursum( number – 1 ).
- Call the recursive function recursum() and print the returned value.
Let’s implement the above logic using Python Language.
Python Code
def recursum(number): if number == 0: return number return number + recursum(number-1) number, sum = 6,0 print(recursum(number))
Output
21
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
N = int(input(“Enter the first ‘N’ natural no. : “))
sum = 0
for i in range(1,N+1):
sum = sum + i
print(f”sum of first {N} natural numbers is {sum}”)
a=int(input(“enter the range “))
sum=0
for i in range(a+1):
sum=sum+i
print(sum)
a=int(input())
b=a*(a+1)//2
print(“Sum is:”,b)