How to Add Digits of the Number Using Single Statement ?

January 11, 2025 No Comments » Hits : 325





Add Digits of the Number Using Single Statement :

#include<stdio.h>
void main()
{
int number=12354;
int sum=0;
for(;number > 0;sum+=number%10,number/=10); 
printf("nSum of the Digits : %d",sum);
}

Output :

15

How ?

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.

Incoming search terms: