 # C Program to find sum of all Integers in a number

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this C program, we have to find out the sum of all integers of the given number. If the number is 250, we have to add the digits of the number (2+5+0) and have the output as 7.

Here what we are doing is, Add the header files into the program to use the built-in input and output functions. After that, accept an integer from the user and save that integer as long int in a variable called 'num'.

Now Copy that integer to a temporary variable temp for keeping the copy of the original number. Open a `while loop` until the number is greater than Zero. Inside that while loop, we extract each digit from the number using the Mod operator and add it to the Sum. Then divide the number by 10 to remove the last digit.

Finally, we display the value of the Sum variable as a result of using a long integer.

## What is the syntax of a while loop?

``````
while (testExpression)
{

// codes inside the body of a while

}
```
```

Here `while loop` evaluates the test expression, if the test expression is True, We execute the code inside the `while loop`. This process continues until the test expression is False.

### ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the variable num, temp, digit, sum as `long integer`, and set sum=0.

STEP 3: Read the number from the user and save it into the variable num.

STEP 4: Assign temp=num.

STEP 5: By using a `while loop` with the condition num>0, calculate digit=num, sum=sum+digit, num=num/10.

STEP 6: Display the given number as 'temp'.

STEP 7: Display Sum of digits as a sum.

## C Source Code

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

void main() {
int num, temp, digit, sum = 0; /* declares the variables for the program */
printf("Enter the number\n");
scanf("%d",& num); /* accepting and storing the number in a variable */
temp = num;
while (num > 0) /*   initialize the while loop */ {
digit = num%10; /* number is mod by 10 to get reminder and sum is added */
sum = sum + digit;
num =num/ 10;
}
printf("Given number =%d \n", temp);
printf("Sum of the digits %d =%d \n", temp, sum);
} /* End of main()*/```
```

## OUTPUT

```Enter the number
123456

Given number =123456
Sum of the digits 123456 =21```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 