fork download
  1. #include <stdio.h>
  2.  
  3. #define BASE 10
  4.  
  5. int main() {
  6. char user_input[5];
  7. int x;
  8.  
  9. printf("Enter a number:");
  10. if (fgets(user_input, sizeof(user_input), stdin)) {
  11. if (sscanf(user_input, "%d", &x) == 1) {
  12. int sum = 0;
  13. while (x > 0) {
  14. sum += x % BASE;
  15. x /= BASE;
  16. }
  17. printf("Sum: %d\n", sum);
  18. }
  19. }
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 2172KB
stdin
4567
stdout
Enter a number:Sum: 22