programming-examples/python/Basics/Python Program to Find Sum of Natural Numbers Using Recursion.py
2019-11-18 14:44:36 +01:00

20 lines
493 B
Python

# Python program to find the sum of natural numbers up to n using recursive function
def recur_sum(n):
"""Function to return the sum
of natural numbers using recursion"""
if n <= 1:
return n
else:
return n + recur_sum(n-1)
# change this value for a different result
num = 16
# uncomment to take input from the user
#num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))