Q: If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)
#include <stdio.h>
int main()
{
int num, sum;
printf("enter any number : ");
scanf("%d",&num);
while(num != 0) /*condition used unti num becomes zero*/
{
/* modulo operator is used to find remainder*/
sum += num%10; /* sum is increase with new remainder after the no is divide by 10 */
num = num/10;
}
printf("sum of digits of given no is %d",sum);
return 0;
}
-----------------------------------------
output :
enter any number : 12345
sum of digits of given no is 15
Comments
Post a Comment