Sunday, 20 May 2012

Write a program to get the sum of digits of a given number using while loop


//program to get the sum of digits of a given number using while loop
void main()
{
int num,sum=0,a,b;
clrscr();
printf("Enter any number to get the sum of its digits ");
scanf("%d",&num);
a=num;
while(a>0)
{
b=a%10;
sum=sum+b;
a=a/10;
}
printf("\nThe sum of digits of %d is %d",num,sum);
getch();
}
Output
Enter any number to get the sum of its digits 1234

The sum of digits of 1234 is 10

No comments:

Post a Comment