fork(1) download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int i, a, num = 32765, n;
  6. int sum = 0;
  7.  
  8. // extract 1st digit
  9. a = num % 10; // a is 5 (% returns the remainder of the division)
  10. n = num / 10; // n is 3276 (should be 3276.5, but int eats 0.5)
  11. sum = sum + a; // sum is 5 which is (0 + 5)
  12.  
  13. // extract the remaining 4 digits
  14. for (i = 0; i < 4; i++) // i is 0, 1, 2, 3
  15. {
  16. a = n % 10; // a is 6, 7, 2, 3
  17. n = n / 10; // n is 327, 32, 3, 0
  18. sum = sum + a; // sum is 11, 18, 20, 23
  19. }
  20.  
  21. printf("the sum of five digits is %d", sum);
  22. return 0;
  23. }
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
the sum of five digits is 23