148. Python Program to find the Sum of n Natural numbers using Recursion.
How does this program work?
- In this program we are going to learn about how to find the Sum of n natural numbers using Python.
- The integer entered by the user is stored in variable n.
- Declare variable sum to store the sum of numbers and initialize it with 0.
- By using Recursion method we can find the sum of n natural numbers.
Here is the code
def sum(n):
if n <= 1:
return n
else:
return n + sum(n-1)
num = int(input("Enter a number: "))
print( "The sum is: ", sum(num))