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 classmethod()
Python Program to Find the Factors of a Number
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String rindex()
Python String startswith()
Python Program to Display the multiplication Table
Python Program to Differentiate Between type() and isinstance()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Type Conversion and Type Casting
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Safely Create a Nested Directory
Python Object Oriented Programming
Python Program to Find LCM
Python Program to Count the Number of Digits Present In a Number
Python Program to Trim Whitespace From a String
Python Tuple index()
Python locals()
Python Program to Get the Full Path of the Current Working Directory
Python issubclass()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python List count()
Python min()
Python String rsplit()
Python Program to Get the Last Element of the List
Python Operators
Python Program to Merge Two Dictionaries
Python Program to Find Factorial of Number Using Recursion
Python Data Structures and Algorithms - Benjamin Baka
Python List index()
Python datetime
Python Program to Add Two Numbers