Friday, 10 January 2014

C Program to Add digits of the number using single statement

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.

No comments:

Post a Comment