# C Program to Find and Print Sum of all Digit of any Number

Here we will learn about how to create a program in C that will ask from the user to enter any number (at run-time) as input, and then calculate and print the sum of all the digit present in that given number. Here is the program:

```// Write a program in C to print the sum of
// digit of any given Number by user
// -----------codescracker.com-----------

#include<stdio.h>
#include<conio.h>
int main()
{
int num, sum=0, rem;
printf("Enter any number: ");
scanf("%d", &num);
while(num>0)
{
rem = num%10;
sum = sum + rem;
num = num/10;
}
printf("\nSum of Digit = %d", sum);
getch();
return 0;
}```

As the program was written under Code::Blocks IDE, therefore after successful build and run, here is the output: Supply any number say 2358 and press ENTER key to see the sum of all the digit as output or result. Here is the second snapshot of the sample run: Here are the list of some main steps used in above program:

• Receive any number from user at run-time as input
• Create a while loop that runs until the number is greater than 0
• Inside the while loop, find the digit of the number by applying remainder or modulous (%) operator, that is use num%10 which gives the last digit of the number and will be initialized to the variable rem. Initialize sum+rem to sum variable. Never forgot to initialize 0 to sum at start of the program. And then divide the number with 10 and continue to check and do the same operation
• Let's suppose that the user has supplied 247 as input
• The variable num holds the value 247 which is greater than 0, then program flow goes inside the while loop, and num%10 or 247%10 or 7 will be initialized to rem. And sum+rem or 0+7 or 7 will be initialized to the variable sum. And finally num/10 or 247/10 or 24 will be initialized to num
• At second run of the while loop, num holds 24 which is again greater than 0, then the program flow again goes inside the while loop and do the same task in same way as done at first run of the while loop
• At last that is after coming out from the while loop, the variable sum holds the value that is equal to the sum of all the digit of that given number by user at start of the program

Tools
Calculator