;

Python Program to Find the Sum of Natural Numbers Using Recursion


Tutorialsrack 22/04/2020 Python

In this Python program, we will learn how to find the sum of the natural numbers using recursion. Natural numbers are the positive integers or non-negative integers that start from 1 and end at infinity, such as 1,2,3,4,5,6,7,8,9,10,……,∞. 

Here is the code of the program to find the sum of the natural numbers using recursion.

Python Program to Find the Sum of Natural Numbers Using Recursion
# Python Program to Find the Sum of Natural Numbers Using Recursion

# Define a Function
def recursion_sum(n):
   if n <= 1:
       return n
   else:
       return n + recursion_sum(n-1)

# Take the Input From the User
num = int(input("Enter the Number: "))
holdNum = num

if num < 0:
   print("Enter a positive number")
else:
   print("The Sum of First",holdNum,"Natural Number is: ", recursion_sum(num))
Output

Enter the Number: 10

The Sum of First 10 Natural Number is:  55

 

Enter the Number: 50

The Sum of First 50 Natural Number is:  1275


Related Posts



Comments

Recent Posts
Tags