Find Sum of Natural Numbers in Python Using Recursion

(Last Updated On: 30/11/2022)

Python Program to Find the Sum of Natural Numbers Using Recursion

# Find Sum of Natural Numbers in Python Using Recursion

def rsum(n):
	if n <= 1:
    	return n
	else:
    	return n + rsum(n-1)

# we are taking a number from user as input
# entered value will be converted to int from string
num = int(input("Enter a number: "))

#accepts a number as input from user and sends it as argument to rsum() function
ttl=rsum(num)
print("The sum is",ttl)

Output:

Enter a number: 15
The sum is 120

Leave a Reply

Your email address will not be published. Required fields are marked *