We have to develop a Python program to find the sum of N natural numbers. Sum of natural number N as given as sum = 1+2+3+4+5+….+(N-1)+N. We can use the while or for loop to write the program. We can also develop a Python program without using the loop.
Examples:-
1+2+3+4+5+6 = 21
1+2+3+4+5+6+7+8+9+10 = 55
Python Program to Find Sum of N Natural Numbers
This python program using a while loop to find the sum of natural numbers. We will take a natural number while declaring the variables. Python program to find the sum of n natural numbers using while loop and finally, the result will be displayed on the screen.
# Python program to find sum of n natural numbers
# take input
num = int(input('Enter a number: '))
# find sum of natural number
sum = 0
x = 1
while x <= num:
sum += x
x += 1
# display result
print('The sum of natural number =', sum)
Output for the different input values:-
Enter a number: 5
The sum of natural number = 15
Enter a number: 10
The sum of natural number = 55
Enter a number: 239
The sum of natural number = 28680
Python Program to Find Sum of N Natural Numbers using For loop
In the previous program, we will use the while loop but in this program, find the sum of n natural numbers using for loop.
# Python program to find sum of n natural numbers
# take input
num = int(input('Enter a number: '))
# find sum of natural number
sum = 0
for x in range (1, num+1):
sum += x
# display result
print('The sum of natural number =', sum)
Output:-
Enter a number: 25
The sum of natural number = 325
Find the Sum of N Natural Numbers using Function
We can also take the help of a user-defined function. A function is a block of code that performs a specific task.
# Python program to find sum of n natural numbers using function
def findSum(num): #user-defined function
sum = 0
x = 1
while x <= num:
sum += x
x += 1
return sum
# take input
num = int(input('Enter a number: '))
# display result
print('The sum of natural number =', findSum(num))
Output:-
Enter a number: 83
The sum of natural number = 3486
Sum of N Natural Numbers using Recursion
We can also use the recursion technique to find the sum of n natural numbers. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.
# Python program to find sum of n natural numbers using recursion
def findSum(num): #user-defined function
if(num == 0):
return num
else:
return (num + findSum(num - 1))
# take input
num = int(input('Enter a number: '))
# display result
print('The sum of natural number =', findSum(num))
Output:-
Enter a number: 325
The sum of natural number = 52975
Enter a number: 1000
RuntimeError: maximum recursion depth exceeded in comparison
Other Efficient Program
This python program is very smallest and easy to understand. In this program, We will use only one formula to find the sum of natural numbers.
Formula:- n * (n+1) / 2
Examples:-
1+2+3+4+5+6 = 6 * (6+1) / 2 = 3 * 7 = 21
1+2+3+4+5+6+7+8+9+10 = 10 * (10+1) / 2 = 5 * 11 = 55
# Python program to find sum of n natural numbers
def findSum(num): #user-defined function
return num * (num+1) / 2
# take input
num = int(input('Enter a number: '))
# display result
print('The sum of natural number =', findSum(num))
Another Program
The above program causes an overflow, even if the result is not beyond the integer limit. We can avoid overflow up to some extent by doing division first.
# Python program to find sum of n natural numbers
def findSum(num): #user-defined function
if (num % 2 == 0) :
return (num/2) * (num+1)
# If num is odd, (n+1) must be even
else :
return ((num+1) / 2) * num
# take input
num = int(input('Enter a number: '))
# display result
print('The sum of natural number =', findSum(num))
Also See:- Sum of Digits of a Number in Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!