C Online Compiler
Example: Sum of Digits of a Number in C using While loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Digits of a Number in C using While loop #include <stdio.h> // It's the main function of the program int main() { int num, sum = 0, mod; // Step-1 Enter a Number printf("Enter Number: "); scanf("%d", &num); // Step-2 Iterate over the input number to get each digit and make the sum of each digit while (num > 0) { mod = num % 10; sum += mod; num = num / 10; } // Step-3 Final output of the program printf("\nOutput: %d", sum); return 0; }
9876
Output
Clear
ADVERTISEMENTS