C Program to Find the Sum of Digits of a Number Using Recursion

Introduction

The sum of the digits of a number is the sum of all individual digits within the number. For example, the sum of the digits of 123 is (1 + 2 + 3 = 6). This guide will show you how to write a C program to find the sum of the digits of a number using recursion.

Example:

  • Input: 1234
  • Output: 10 (since (1 + 2 + 3 + 4 = 10))

Problem Statement

Create a C program that:

  • Takes an integer number as input from the user.
  • Uses a recursive function to calculate the sum of the digits.
  • Displays the result.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Recursive Function: Define a recursive function that calculates the sum of the digits of the number.
  3. Write the Main Function: Define the main function to take user input, call the recursive function, and display the result.
  4. Input the Number: Use scanf to take input from the user for the number.
  5. Call the Recursive Function: Pass the number to the recursive function to calculate the sum of its digits.
  6. Display the Result: Use printf to display the sum of the digits.

C Program to Find the Sum of Digits of a Number Using Recursion

#include <stdio.h>

// Step 2: Define the recursive function to calculate the sum of digits
int sumOfDigits(int n) {
    if (n == 0) {
        return 0;  // Base case: If the number is 0, return 0
    } else {
        return (n % 10) + sumOfDigits(n / 10);  // Recursive case
    }
}

int main() {
    // Step 3: Declare a variable to hold the number
    int num;

    // Step 4: Prompt the user to enter the number
    printf("Enter a number: ");
    scanf("%d", &num);

    // Step 5: Call the recursive function to calculate the sum of digits
    int result = sumOfDigits(num);

    // Step 6: Display the result
    printf("Sum of digits of %d is %d\n", num, result);

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Define the Recursive Function

  • The sumOfDigits function takes an integer n as input.
  • Base Case: If n is 0, the function returns 0 (since there are no more digits to add).
  • Recursive Case: If n is greater than 0, the function returns the last digit of n (obtained using n % 10) plus the sum of the digits of the remaining number (obtained using n / 10), which is calculated recursively.

Step 3: Declare a Variable

  • The variable num is declared to store the input number.

Step 4: Input the Number

  • The program prompts the user to enter a number using scanf.

Step 5: Call the Recursive Function

  • The program calls the sumOfDigits function with num as the argument and stores the result in the variable result.

Step 6: Display the Result

  • The program uses printf to display the sum of the digits of the input number.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a number: 1234
Sum of digits of 1234 is 10

Example 2:

Enter a number: 567
Sum of digits of 567 is 18

Example 3:

Enter a number: 9
Sum of digits of 9 is 9

Conclusion

This C program demonstrates how to calculate the sum of the digits of a number using a recursive function. It covers basic concepts such as recursion, base cases, and user input, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top