Pages

Saturday, February 22, 2014

c program for sum of digits

PROGRAM:

#include
#include
void main()
{
int a,sum=0,k;
printf("\nEnter the number:  ");
scanf("%d",&a);
while(a>10)
{
       k=a%10;
       sum=sum+k;
       a=a/10;
      
}
sum=sum+a;
 printf("%d",sum);
 getch();
}


OUTPUT:




EXPLANATION:


This program has simple logic

If you given number is 345

Then it can be divide by 10  and gave by reminder as 5, it can be add with the variable of sum,

The quotient value 34 can store again the variable of a,

The while loop can be continue until the value of a <10 p="">
The finial quotient value will be less than 10, it should add with the sum.


No comments:

Post a Comment