In this program, you’ll learn to find the sum of natural numbers using recursive function.
To understand this example, you should have the knowledge of the following Python programming topics:
In the program below, we’ve used a recursive function recur_sum() to compute the sum up to the given number.
Source Code
# Python program to find the sum of natural using recursive function
def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)
# change this value for a different result
num = 16
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
Output
The sum is 136
Note: To test the program for another number, change the value of num.
Related posts:
Python String isupper()
Python Program to Find the Largest Among Three Numbers
Python Set add()
Python datetime
Python Program to Check If a List is Empty
Python enumerate()
Python String capitalize()
Python range()
Python String rfind()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python *args and **kwargs
Python Object Oriented Programming
Python List clear()
Python Program to Iterate Over Dictionaries Using for Loop
Python Closures
Python any()
Python Dictionary values()
Python Program to Find the Square Root
Python tuple()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python isinstance()
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Catch Multiple Exceptions in One Line
Python Sets
Python Program Read a File Line by Line Into a List
Python Program to Differentiate Between type() and isinstance()
Python break and continue
Python Dictionary keys()
Python vars()
Python String center()
Python Program to Split a List Into Evenly Sized Chunks
Python List append()