C Online Compiler
Example: Sum of Digits of a Number in C using For loop
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Digits of a Number in C using For 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 for (int n = num; n > 0; n = n / 10) { mod = n % 10; sum += mod; } // Step-3 Final output of the program printf("\nOutput: %d", sum); return 0; }
9876
Output
Clear
ADVERTISEMENTS