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 Trim Whitespace From a String
Debug a JavaMail Program
Python Program to Iterate Through Two Lists in Parallel
Python min()
Python Input, Output and Import
Python Program to Concatenate Two Lists
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String translate()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String find()
Python list()
Python Program to Find the Largest Among Three Numbers
Python Program to Print Colored Text to the Terminal
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Check the File Size
Deep Learning in Python - LazyProgrammer
Python String format_map()
Python List extend()
Python Tuple
Python Matrices and NumPy Arrays
Python Dictionary popitem()
Python callable()
Python Set discard()
Python String istitle()
Python Machine Learning - Sebastian Raschka
Python Program to Count the Occurrence of an Item in a List
Python len()
Python Program to Compute the Power of a Number
Python getattr()
Python Set intersection_update()
Python String lower()
Deep Learning with Applications Using Python - Navin Kumar Manaswi