C Program to Find the Sum of Natural Numbers using Recursion

Created with Sketch.

C Program: Sum of Natural Numbers using Recursion

Recursion is a programming concept where a function calls itself, and it’s often used to solve problems that can be broken down into smaller, identical subproblems. In this blog post, we’ll explore a C program that uses recursion to find the sum of natural numbers. The post will provide a comprehensive explanation along with a step-by-step guide and example outputs.

Understanding the Sum of Natural Numbers

 

C Program: Sum of Natural Numbers using Recursion

Let’s proceed with the C program. The example includes a function named calculate_sum that recursively calculates the sum of natural numbers.

#include <stdio.h>

// Function to calculate the sum of natural numbers using recursion
int calculate_sum(int num) {
    if (num == 0) {
        return 0; // Base case: sum of first 0 natural numbers is 0
    } else {
        return num + calculate_sum(num - 1); // Recursive case
    }
}

int main() {
    int number;

    // Get user input
    printf("Enter a positive integer: ");
    scanf("%d", &number);

    // Check for negative input
    if (number < 1) {
        printf("Sum is undefined for non-positive integers.\n");
    } else {
        int result = calculate_sum(number);
        printf("The sum of natural numbers from 1 to %d is: %d\n", number, result);
    }

    return 0;
}

Example Usage

Compile the C program:

gcc sum_of_natural_numbers.c -o sum_of_natural_numbers

Run the compiled program:

./sum_of_natural_numbers

Enter a positive integer when prompted. The program will then calculate and display the sum.

Example Outputs

  • Sum of Natural Numbers up to 5:

Enter a positive integer: 5
The sum of natural numbers from 1 to 5 is: 15

Sum of Natural Numbers up to 10:

 
Enter a positive integer: 10
The sum of natural numbers from 1 to 10 is: 55

Sum of Natural Numbers up to 0:

 
Enter a positive integer: 0
The sum of natural numbers from 1 to 0 is: 0

Step-by-Step Explanation

  1. Function calculate_sum():

    • The function takes an integer parameter (num) and recursively calculates the sum.
    • The base case checks if num is 0, in which case the sum is 0.
    • If num is greater than 0, the function adds num to the sum of (num - 1).
  2. User Input:

    • The program uses scanf to obtain user input for a positive integer.
  3. Negative Number Check:

    • The program checks if the input is non-positive and prints a message if so.
  4. Result Display:

    • If the input is positive, the program calculates and prints the sum.

Conclusion

Recursion is a powerful technique in programming, and calculating the sum of natural numbers is a classic problem to showcase its application. Understanding recursion not only helps in solving specific problems but also enhances problem-solving skills in general. As you explore this C program, consider how recursion simplifies the solution to a problem that can be divided into smaller, similar subproblems.

Leave a Reply

Your email address will not be published. Required fields are marked *