In this program, you'll learn to find the sum of natural numbers using recursive function.

To understand this example, you should have the knowledge of following Python programming topics:

In the program below, we've used a recursive function `recur_sum()`

to compute the sum up to the given number.

# Python program to find the sum of natural numbers up to n using recursive function def recur_sum(n): """Function to return the sum of natural numbers using recursion""" if n <= 1: return n else: return n + recur_sum(n-1) # change this value for a different result num = 16 # uncomment to take input from the user #num = int(input("Enter a number: ")) if num < 0: print("Enter a positive number") else: print("The sum is",recur_sum(num))

**Output**

The sum is 136

**Note:** To test the program, change the value of `num`

. Try negative numbers as well.

It takes a lot of effort and cost to maintain Programiz. We would be grateful if you support us by either:

**Disabling AdBlock on Programiz. We do not use intrusive ads.**

or

Donate on Paypal
By using Programiz, you agree to use cookies as stated in our Privacy policy Continue