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 Dictionary update()
APIs in Node.js vs Python - A Comparison
Python String translate()
Python List Comprehension
Python Set intersection()
Python Matrices and NumPy Arrays
Python Program to Add Two Matrices
Python frozenset()
JavaScript Recursion and stack
Python Program to Merge Two Dictionaries
Python RegEx
Python Set pop()
Python String expandtabs()
Python List insert()
Python Program to Check If a List is Empty
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Display Fibonacci Sequence Using Recursion
Python Dictionary fromkeys()
Python Object Oriented Programming
Python Program to Display Powers of 2 Using Anonymous Function
Python property()
Python Program to Check If Two Strings are Anagram
Python Program to Check if a Number is Odd or Even
Python Program to Find Armstrong Number in an Interval
Python Program to Differentiate Between del, remove, and pop on a List
Python all()
Debug a JavaMail Program
Python Program to Display Calendar
Python Directory and Files Management
Python Program to Swap Two Variables
Python Dictionary
Python Global Keyword