Skip to content
Home » Python recursive function to compute sum of numbers from 1 to n

Python recursive function to compute sum of numbers from 1 to n

Program to illustrate recursion :

def compute(num):
    if (num==1):
        return 1
    else:
        return(num+compute(num-1))

# __main__
n=int(input("Enter number:"))
s=compute(n)
print("The sum of the series from 1 to ",n,"is",s)

Output:

Enter number:6
The sum of the series from 1 to 6 is 21