Write a program to Find Sum of Natural Numbers Using Recursion in Python
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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) #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)) |