Python Examples

Python Program to Find the Sum of Natural Numbers Using Recursion 

Recursion is the process in Python through which a function can call itself directly or indirectly. The function that is used in the process of recursion is referred to as the recursion function. It is used for various applications. One of the simplest examples of the recursive function is the sum of natural numbers. To understand how recursive sum Python code works, one should have a clear concept of the function and if…else conditional statement 

Source Code 

                    

def nat_sum (n):

              if n <= 1:

                             return n

              else:

                             return n + nat_sum (n-1)

num = int (input (“Enter the number until which you want the sum to be performed.”)

if num < 0:

              print (“Please enter a valid input.”)

else:

              print (“The sum of the natural numbers is “, nat_sum (num))

The program above first has a function named nat_sum that performs the sum of natural numbers using recursion. The parameter passed in the function is the number until which all the natural numbers need to be added.  

If the number input by the user is less than 1, then the function will return that it is an invalid input and will not perform a sum. If the number is greater than 1, then all the natural numbers that fall between 1 and the number input will be added to the number itself. The recursion function is called until the number being added reaches 0. 

Thus, with the help of a function that calls itself, we can perform the sum of n natural numbers. Note that if there is no conditional statement applied in the function, then the recursive function will continue to iterate infinitely. Thus, with the help of a condition, one can break the recursive function as per the problem and algorithm of the code. 

Frequently Asked Questions 

Q1. What is a recursive digit sum?

Recursive sum Python is the code through which a recursive function, that is, a function that calls itself, is used to continuously iterate and provide the sum of n natural numbers. 

If the user wants, then by changing the condition of the if…else statement used in the recursive function can be changed to perform the sum of any numbers or integers. 

Q2. How do you find the sum of recursive numbers?

To find the sum of recursive numbers, a recursive function is coded that includes the required condition. The function is called by itself until the condition provided is true. Once it is false, the function will return the calculated value to the main function and print it. 

Q3. How do you sum a loop in Python?

To use a loop to find the sum of natural numbers in Python, the FOR or WHILE loop is used and iterated until all the numbers lying between 1 and the number provided are added to the sum.  

Q4. What is a recursive function in Python?

The recursive function in Python is a function that calls itself directly or indirectly. Thus, a function that is defined can call to itself as many times as required.   

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.