C Program to Calculate Sum of Digits
The sum of digits of a number is the total obtained by adding each digit individually. For example, the sum of digits of 1234 is 1 + 2 + 3 + 4 = 10. This is a fundamental exercise in C programming that teaches loops, arithmetic operations, and number manipulation.
In this tutorial, we will learn different approaches to calculate the sum of digits, including while loops, for loops, and recursion, along with step-by-step explanations and common mistakes to avoid.
Algorithm to Calculate Sum of Digits
1. Read an integer number from the user and store it in variable num.
2. Initialize a variable sum = 0 to store the total sum of digits.
3. Use a loop (while or for) to process the number until it becomes zero.
4. Inside the loop, extract the last digit using remainder = num % 10.
5. Add the remainder to sum using sum += remainder.
6. Remove the last digit from num using num /= 10.
7. Repeat steps 4-6 until num becomes 0.
8. Print the final value of sum.
1. Sum of Digits Using While Loop
We use a while loop to repeatedly extract and add digits until the number becomes zero.
#include <stdio.h>
int main() {
int num, sum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
remainder = num % 10;
sum += remainder;
num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Enter a number: 1234 Sum of digits = 10
2. Sum of Digits Using For Loop
The for loop can also be used to perform the same calculation. The loop initialization and update steps handle the digit extraction and number reduction.
#include <stdio.h>
int main() {
int num, sum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
for(; num != 0; num /= 10) {
remainder = num % 10;
sum += remainder;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Enter a number: 9876 Sum of digits = 30
3. Sum of Digits Using Recursion
Recursion can be used to break the problem into smaller subproblems, where each call adds the last digit to the sum of the remaining digits.
#include <stdio.h>
int sumOfDigits(int n) {
if(n == 0)
return 0;
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits = %d\n", sumOfDigits(num));
return 0;
}
Enter a number: 1234 Sum of digits = 10
4. Common Mistakes
1. Forgetting to divide the number inside the loop (num /= 10) → results in an infinite loop.
2. Using float or double instead of int → modulus operator (%) only works with integers.
3. Handling negative numbers incorrectly → solution: use num = abs(num);
#include <stdio.h>
int main() {
int num, sum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0) {
remainder = num % 10;
sum += remainder;
// Missing num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
5. Advanced Variations
1. Sum of Odd Digits Only:
while(num != 0) {
remainder = num % 10;
if(remainder % 2 != 0)
sum += remainder;
num /= 10;
}
2. Sum of Even Digits Only:
while(num != 0) {
remainder = num % 10;
if(remainder % 2 == 0)
sum += remainder;
num /= 10;
}
3. Handling negative numbers: use num = abs(num) from stdlib.h to convert input to positive.
6. Applications of Sum of Digits
1. Digital root calculations in number theory.
2. Checksum and validation of numbers like credit cards and ISBN numbers.
3. Numerology and other educational exercises.
4. Foundational logic for algorithms like number reversals, palindrome checking, and factorial digit sums.
Conclusion
Calculating the sum of digits in C is a simple yet powerful exercise for beginners and intermediate programmers. It strengthens understanding of loops, recursion, arithmetic operations, and problem-solving. With careful handling of edge cases like negative numbers and large values, this program can serve as a foundation for more advanced numerical algorithms.
Codecrown