Python Program to Find the Sum of First N Natural Numbers
Find the Sum of The First N Natural Numbers in Python
Given an integer input the objective is to write a code to Find the Sum of First N Natural Numbers in C++. To do so we simply keep adding the value of the iter variable using a for loop.
Example Input : num = 8
Output : 36 Where first 8 number is 1, 2, 3, 4, 5, 6, 7, 8 Sum of numbers = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
Find the Sum of the First N Natural Numbers in Python
Given an integer input of N, the objective is to find the sum of all the natural numbers until the given input integer. To do so we can use different approaches to write the Python code and some such methods are mentioned below,
- Method 1 : Using for Loop
- Method 2 : Using Formula for the Sum of Nth Term
- Method 3 : Using Recursion
We’ll discuss and learn more about each above mentioned method in detail in the sections below.
Method 1 : Using for Loop
In this method we’ll add all the natural numbers until the given integer input using for loop in Python.
Python Code
num = 5 sum = 0 for i in range(num+1): sum+=i print(sum)
Output
15
Working
For a user input num.
- Initialize a variable sum = 0.
- Using a for loop in iteration ‘i’ iterate between [1, num].
- Each time add ‘i’ to current sum as sum = sum + i.
- Print sum.
Explanation
Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value.
The algorithm for the above code is as follows,
- Import the required module using the import keyword.
- Initialize the required variables.
- Run a for loop with range as N+1.
- Keep adding the iter values to the Sum variable.
- Print Sum variable using print() function.
The output for the above mentioned code is the sum of all the natural numbers until the given value.
Method 2 : Using Formula for the Sum of Nth Term
In this Method we use the formula for finding the sum of N term.
Python Code
num = 5 print(int(num*(num+1)/2))
Output
15
Working
For a user input n.
- Initialize a variable sum = 0.
- Use formula sum = n(n+1)/2.
- Print sum
Explanation
Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value.
The algorithm for the above code is as follows,
- Import the required modules using the import keyword.
- Initialize the required variables.
- Run a for loop with range as N+1.
- Keep adding the iter values to the Sum variable.
- Print Sum variable using print() function.
This algorithm uses the formula n(n+1)/2 that can be used to find sum of first N natural numbers. This also reduces the time complexity from O(n) to O(1). The output for the above mentioned code is the sum of all the natural numbers until the given value.
Method 3 : Using Recursion
This method uses Recursion to recursively add the natural numbers up to the given integer input using recursion in c++.
Python Code
def getSum(num): if num == 1: return 1 return num + getSum(num-1) num = 5 print(getSum(num))
Output
15
Working
For a user input n.
- Initialize a variable sum = 0.
- Call function getSum (num).
- In each recursive call add the current value of n and call for lower recursion call using return num + getSum(num-1);
- Print sum value
Explanation
Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we recursively call a function iterate through all the numbers that lay within N and keep incrementing the sum value.
The algorithm for the above code is as follows,
- Import the required modules using the import keyword.
- Define a Recursive function getSum() which takes the number input as an argument.
- Recursively call the function and keep on adding the return statements.
- Initialize the required variables.
- Call the Recursive function and print out the returned value using cout keyword.
The output for the above mentioned code is the sum of all the natural numbers until the given value.
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
Login/Signup to comment
num = int(input(“ENTER THE NUMBER:—- “))
sum = (num*(num+1))/2
print(“THE SUM OF NATURAL NO IS :— “,sum)
#find the sum of first n natural numbers
#user input
n = int(input(‘Enter your number:’))
#function declaration
def SumOfNaturalNos(n):
res = 0
for i in range(1,n+1):
res += i
return res
#function calling
print(SumOfNaturalNos(n))
x = int(input(“give first no:”))
y = int(input(“give second numbber:”))
z = ((y * (y + 1)) / 2) – ((x * (x + 1)) / 2) + x
print(“sum is : {}”.format(int(z)))
a = int(input(“Enter The Number To Find:\n”))
l = [i for i in range (1,a+1)]
print(sum(l))
Easy code for sum of numbers of given range
start=int(input(“enter the no=”))
end=int(input(“enter the no=”))
sum=0
for i in range(start, end+1):
sum+=i
print(sum)
Method 2 is Not working:
num = int(input(“Enter the Number:”))
sum = (num/2)*(num+1)
print(“The Sum of N natural Number is {}”.format(sum))
Here is the code to find the sum of numbers in a range (a,b):-
a=int(input())
b=int(input())
print(int(((b*(b+1))/2)-((a*(a+1))/2)))
num=int(input(“enter the number:”))
sum=0
for i in range(0,num):
sum = sum + i
print(“sum of natural numbers is “+ str(sum))
def sum(n,m):
if m == n:
return n
return sum(n,m-1)+m
ans = sum(4,6)
print(ans)
#n lower, m upper
number = int(input(“Enter the Number: “))
x = 0
for i in range(1,number+1):
print(i)
x += i
print(“Sum of Number is “,x)
x=int(input(“Enter the starting number: “))
y=int(input(“Enter the ending number: “))
c=0
for i in range(x,y+1):
c=c+i
print(c)
This code is for Sum of n natural number using function
def sumOfnumber(n):
if n==0:
return 0
else:
return n+ sumOfnumber(n-1)
print(sumOfnumber(8))