C Program to Add digits of the number using single statement
Add Digits of the Number Using Single Statement :
1 2 3 4 5 6 7 8 9 10 | #include<stdio.h> int main() { int number = 12354; int sum = 0; for (; number > 0; sum += number % 10, number /= 10); printf("\nSum of the Digits : %d", sum); } |
Output :
1 | 15 |
How ?
1 2 3 4 | for(initialization ; conditional ; increment) { //body } |
- In ‘For Loop‘ Condition is first tested and then body is executed.
- Carefully Look at Semicolon at the end of ‘For Loop’ , which tells us two Things -
- For Loop is Bodyless.
- Only Condition and Increment Statements will be executed.