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 :: append dictionary python 
Python :: Create list of unique values from dictionary 
Python :: python integer to string format 
Python :: python import 
Python :: args and kwargs 
Python :: concatenate list in python 
Python :: can a function output be save as a variable python 
Python :: remove extra blank spaces 
Python :: django jsonresponse 
Python :: pyspark filter column in list 
Python :: turtle python 
Python :: Python Read the CSV file 
Python :: lemmatization in nlp 
Python :: tkinter change button color smoothly 
Python :: Python how to search in string 
Python :: PHP echo multiple lines example Using Nowdoc 
Python :: how to create a string in python 
Python :: logical operators in python 
Python :: python data type conversion 
Python :: No installed app with label 
Python :: python sort 2d list different sort order for different columns 
Python :: vigenere cipher with all printable characters python 
Python :: python datetime with day date suffix format 
Python :: inherit functions from other classes 
Python :: change a color on touch roblox 
Python :: change gles3 to gles2 
Python :: cmake python interpreter 
Python :: pyqt button hover color 
Python :: python decomposition facteur premier 
Python :: how to combine two lists in one python 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =