# Determining sum of digits using C

In this article we will discus about the program to find sum of digits using various method of C programming language.

For example, if a user gives the input as 123 then we will calculate the sum of all the digits like 1 + 2 + 3 = 8 and try to print the output 8. We can do this program in different way but we will discus some of the most common ways. Let’s see some program bellow.

## Determining sum of digits of a number

``````// program to determine sum of digits
#include <stdio.h>
int main(){
int num, total, sum = 0, remainder;

printf("Enter the integer here : ");
scanf("%d", &num);

total = num;

while (total != 0){
remainder = total % 10;
sum = sum + remainder;
total = total / 10;
}

printf("\nSum of all the digit of %d is = %d\n", num, sum);

return 0;
}``````

### Output of this sum of digit program ## How this program works?

Suppose, the user gives the input 225. Now, inside while loop this 225 will be divided by 10 which gives the reminder 5. Our initial sum was zero and now

sum = 0 + 5

= 5

And the new number will be 225 / 10 = 22 (because we will get only integer portion as we divide the integer by another integer).

Now, again 22 % 10 = 2 and this 2 will be added with sum. So, sum will be 5 + 2 = 7 and the total will be 22 / 10 = 2.

Again, our new total is 2 and 2 % 10 = 2. sum will be 7 + 2 = 9 and total will be 2 / 10 = 0.

But, when total = 0, out loop will break and the it will print the sum 9.

## Sum of digits using for loop in C

``````// sum of digit using for loop
#include <stdio.h>
int main(){
int number, sum = 0, rem;

printf("Enter any number to find sum of digits : ");

for (scanf("%d", &number); number != 0; number = number / 10) {
rem = number % 10;
sum = sum + rem;
}

printf("\nSum of digits = %d\n", sum);

return 0;
}``````

### Output of program ## Sum of digits using function

``````#include <stdio.h>

if (num == 0){
return 0;
}
return (num % 10 + addDigit(num / 10));
}

int main(){
int num, sum;

printf("Enter an integer : ");
scanf("%d", &num);

sum = addDigit(num);   // calling function

printf("\nSum is = %d\n", sum);

return 0;
}
``````

### You will see the output like this

``````Enter an integer : 455

Sum is = 14

Process returned 0 (0x0)   execution time : 3.499 s
Press any key to continue.``````

## Sum of digits using string in C

``````#include <stdio.h>

int main(){
int a, result, total;
char num;

printf("Input an integer for sum of digits : ");
scanf("%s", num);

result = a = 0;

while (num[a] != '\0') {
total = num[a] - '0';
result = result + total;
a++;
}

printf("Sum is = %d\n", result);

return 0;
}``````

### See the output of sum of digits program This program gives us a very effective facility that when we use integer, long integer or long long integer then everything has the limit of number. You can see the limit from our data types in C chapter.

But when we use string to make a program then we can take a very very big integer. This can only be served by sting in C. Obviously we have to transfer the sting to number as we have done in the above program.

Previous pageLeap year program in C

Next pagePrime number program in C