Question:
Write a python program to find sum of natural numbers using recursion
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Python program to find the sum of natural numbers up to n using recursive function def rsum(n): if n <= 1: return n else: return n + rsum(n-1) num = int(input("Enter a number: ")) if num < 0: print("Enter a positive number") else: print("The sum is",rsum(num)) |
Explanation:
A recursive function (DEF) is a function which either calls itself or is in a potential cycle of function calls.
Output:
1 |
('The sum is', 15) |
Leave a Reply