Loops
Sum of Digits
Find the sum of digits of a number using a while loop.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Find the sum of digits of a number using a while loop.
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
while (n > 0) {
sum += n % 10;
n /= 10;
}
printf("Sum = %d\n", sum);
return 0;
}
1234
Sum = 10