# C Program to find the sum of the digits of a number untill the sum is reduced to a single digit

The following is a C program to find the sum of the digits till the sum is reduced to a single digit.

Expected Output:

1st run:

2nd run:

## How it works

The following table demonstrates what happens a each iteration of the inner while loop, assuming  `num = 12345`:

Iteration num sum rem
After 1st iteration `remainder = 12345%10 = 5 `   `sum = 0+5 = 5` `num = 12345/10 = 1234`
After 2nd iteration `remainder = 1234%10 = 4 `   `sum = 5+4 = 9` `num = 1234/10 = 123`
After 3rd iteration `remainder = 123%10 = 3 `   `sum = 9+3 = 12` `num = 123/10 = 12`
After 4th iteration `remainder = 12%10 = 2 `   `sum = 12+2 = 14` `num = 12/10 = 1`
After 5th iteration `remainder = 1%10 = 1 `   `sum = 14+1 = 15` `num = 1/10 = 0`
`num = sum = 15`

This completes the first iteration of the outer while loop. Since the `num` is still not reduced to single digit, the condition `num / 10 != 0` evaluates to true (`1`) and the body of inner while loop is executed again.

Iteration num sum rem
After 1st iteration `remainder = 15%10 = 5 `   `sum = 0+5 = 5` `num = 15/10 = 1`
After 2nd iteration `remainder = 1%10 = 1 `   `sum = 5+1 = 6` `num = 1/10 = 0`
`num = sum = 6`

The sum is now reduced to a single digit. As a result, the condition `num / 10 != 0`, evaluates to false (`0`) and control comes out of the body of the outer while loop.