Write a recursive function to calculate the sum of first n natural numbers.

amannarayanm · updated September 23, 2021 · Python
def sum(num):
    if num==1 or num==0:
        return 1
    else:
        sum_number = num + sum(num-1)
    return sum_number
    
    
print(sum(10))

Comments

Please sign up or log in to contribute to the discussion.