Sum of digits in C
C program to calculate sum of digits of a number: we will use modulus operator(%) to extract individual digits of a number and keep on adding them.
C programming code
main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of the number = %d\n",sum);
return 0;
}
For example,if the input is 98, sum(variable) is 0 initially