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 Program to Get Line Count of a File
Python Program to Compute all the Permutation of the String
Python List count()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python locals()
Python set()
Python Inheritance
Python List index()
Intelligent Projects Using Python - Santanu Pattanayak
Python Dictionary
Python String lower()
Python Program to Check Armstrong Number
Python Namespace and Scope
Python datetime
Python Directory and Files Management
Python List clear()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python List pop()
Python Program to Create a Countdown Timer
Deep Learning in Python - LazyProgrammer
Python Program to Check If a List is Empty
Python frozenset()
Python Decorators
Python String center()
Python Program to Differentiate Between type() and isinstance()
Python Program to Find the Size (Resolution) of a Image
Python Program to Get the Full Path of the Current Working Directory
Python isinstance()
Python Program to Check If Two Strings are Anagram
Python Multiple Inheritance
Python RegEx
Python Program to Get a Substring of a String