Every number consists of digits. For simplicity, we will consider positive integers only. For example, number 125 contains three individual digits, i.e., 1, 2, and 5. To find the sum of individual digits of a number (input), we need to get and add them.
In your school, you must have learned how to divide two numbers. We divide a number by 10 (divisor), find the remainder, and store it. For instance, the number 2 divided by 10 gives the remainder 2 (the sum of a digit of the number 2). C language has an operator '%' (don't confuse it with the percentage symbol of maths) that returns the remainder. The expression a%b (a and b are two variables) returns the remainder when the variable a is divided by the variable b.
For a single-digit number, the sum of its digits equals the number, but what about an integer that consists of more than a single digit? For example, for 13, we divide by our divisor (10) and get the remainder of 3 (store it in a variable named sumOfDigits used for calculating the sum of digits). Now we divide 13/10 and get 1, again divide by our divisor to get the remainder 1 (add it to the variable sumOfDigits).
The sum of digits C program to calculate the sum of digits of a number, we use the modulus operator (%) to extract individual digits of a number and keep on adding them.
Sum of digits of a number in C
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
If you wish, you can modify the input variable (n) and without using an additional variable (t), but we don't recommend it.
Output of program:
For example, if the input is 98, the variable sum is 0 initially
98%10 = 8 (% is modulus operator, which gives us the remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in C language, when we divide an integer by another, we get an integer.
9%10 = 9
sum = 8 (previous value) + 9
sum = 17
9/10 = 0.
So finally, n = 0, the loop ends; we get the required sum.
Download Add digits program.
C program to find sum of digits using for loop
int main()
{
int n, sum = 0, r;
printf("Enter a number\n");
for (scanf("%d", &n); n != 0; n = n/10) {
r = n % 10;
sum = sum + r;
}
printf("Sum of digits of a number = %d\n", sum);
return 0;
}
Calculate the sum of digits in C without the modulus operator
C program to find the sum of digit(s) of an integer that does not use the modulus operator. Our program uses a character array (string) for storing an integer. We convert every character into an integer and add all these integers.
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
while (n[c] != '\0') {
t = n[c] - '0'; // Converting character to integer
sum = sum + t;
c++;
}
printf("Sum of digits of %s = %d\n", n, sum);
return 0;
}
An advantage of this method is that the input integer can be huge, which we can't store in an int or a long long data type variable. See the example below.
Output of program:
9876543210
Sum of digits of 9876543210 = 45
C program to find the sum of digits of a number using recursion
int add_digits(int);
int main()
{
int n, result;
scanf("%d", &n);
result = add_digits(n);
printf("%d\n", result);
return 0;
}
int add_digits(int n) {
if (n == 0) // Base case
return 0;
return (n%10 + add_digits(n/10));
}