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 super()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Dictionary update()
Python Program to Shuffle Deck of Cards
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Find the Sum of Natural Numbers
Python Program to Sort Words in Alphabetic Order
Python String replace()
Python Set union()
Python enumerate()
Python globals()
Python String endswith()
Python Program to Return Multiple Values From a Function
Python String join()
Python Program to Split a List Into Evenly Sized Chunks
Python Shallow Copy and Deep Copy
Python String count()
Python String isidentifier()
Python range()
Python locals()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String lstrip()
Python String partition()
Python Set isdisjoint()
Python eval()
Python abs()
Python input()
Python Program to Get the Last Element of the List
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Modules
Python type()
Python Set discard()