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
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Recursive Function: Define a recursive function that calculates the sum of the digits of the number.
- Write the Main Function: Define the
mainfunction to take user input, call the recursive function, and display the result. - Input the Number: Use
scanfto take input from the user for the number. - Call the Recursive Function: Pass the number to the recursive function to calculate the sum of its digits.
- Display the Result: Use
printfto 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
sumOfDigitsfunction takes an integernas input. - Base Case: If
nis 0, the function returns 0 (since there are no more digits to add). - Recursive Case: If
nis greater than 0, the function returns the last digit ofn(obtained usingn % 10) plus the sum of the digits of the remaining number (obtained usingn / 10), which is calculated recursively.
Step 3: Declare a Variable
- The variable
numis 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
sumOfDigitsfunction withnumas the argument and stores the result in the variableresult.
Step 6: Display the Result
- The program uses
printfto 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.