Calculate Sum of N Natural Numbers in C using Recursion

Sum-of-N-Natural-Numbers-in-C-using-Recursion

Sum of N Natural Numbers in C using Recursion Program Description:

Write a Program to Calculate the Sum of N Natural Numbers in C using Recursion. The program accepts a number from the user and calculates the sum of the first n natural numbers. We need to use the recursion and should not use the C Loops ( like for, while, and do-while) to calculate the sum.

Example Input and Output:

Input:

Enter a Number: 10

Output:

Sum of first 10 numbers is : 55

📢Related Program: Sum of N Natural Numbers using Loops

What is Recursion:

Function calling itself is called recursion. Any function which calls itself is called a recursive function.

Here is the pseudo-code of recursion.

Pre-Requisites:

Sum of N Natural Numbers in C using Recursion Algorithm / Explanation:

  1. Take the input from the user and store it in the variable named n.
  2. Check if the n is a negative number, If so display an error message ( ERROR! Enter a Valid Number). Ask for the user input again. Take the program control back to the start of the program using the Goto Statement in C
    • Use goto INPUT; and INPUT: labels to jump to the start of the program.
  3. Once we got the positive number, Proceed to the next step.
  4. Create a function called sumOfNumbers. The sumOfNumbers function takes an integer as input and calculates the sum of the first n natural numbers.
    • The sumOfNumbers uses recursion to calculate the sum of n numbers and returns it.
    • The base condition for the recursion is n == 0. So our recursive calls will stop once the formal argument n reaches the value .
    • If the formal argument n value is not equal to zero. Then make a recursive call to the sumOfNumbers function with n-1 as an argument. Also, add the value of the formal argument to the return value of the function call ( n + sumOfNumbers(n-1))
    • The above recursive calls continue until we reach the base condition.
  5. Call the sumOfNumbers function from the main function, Which returns the sum of first n natural numbers. Store the return value in result variable and display the result onto the console using the printf

Sum of N Natural Numbers in C using Recursion Program:

Here is the code for the above algorithm

Here are the prototype details of the sumOfNumbers function

  • int sumOfNumbers(int);
  • function_name: sumOfNumbers
  • argument_list: int
  • return_value : int

Program output:

Compile and run the program.

Test Case 1: Positive Numbers:

generate-Sum-of-N-Natural-Numbers-in-C-using-Recursion-program-output

Test Case 2: Negative Numbers:

As you can see from the above output, We are getting the desired results.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] Calculate Sum of N Natural Numbers in C using Recursion […]

  2. […] C Program to find sum of Fist N Natural Number using Recursion […]

  3. […] C Program to calculate the sum of N Natural Numbers using Recursion […]

Leave a Reply