Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sum of natural numbers recursion

# 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 210
Comment

Python Program to Find Sum of Natural Numbers Using Recursion

# 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))
Comment

PREVIOUS NEXT
Code Example
Python :: how to make a infinite loop in python 
Python :: show all rows python 
Python :: python csv read header only 
Python :: pygame escape key 
Python :: read pickle file python 
Python :: list loop python 
Python :: how to add row in spark dataframe 
Python :: python check if type 
Python :: remove spaces from a list python 
Python :: remove nana from np array 
Python :: python list all files in directory 
Python :: remove outliers numpy array 
Python :: mad python 
Python :: take input in 2d list in python 
Python :: discord get user slash command 
Python :: c vs python 
Python :: how to sort dictionary in python by value 
Python :: ValueError: Shapes (None, 1) and (None, 11) are incompatible keras 
Python :: python trick big numbers visualisation 
Python :: Multiple Box Plot using Seaborn 
Python :: python run a system command 
Python :: run python file in interactive mode 
Python :: how to make a latency command discord.py 
Python :: python add to list with index 
Python :: python max value of list of tuples 
Python :: array as an input in python 
Python :: drop duplicate rows pandas except nan 
Python :: colab add package 
Python :: screen size python 
Python :: mad libs in python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =