fork download
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.  
  6. int number; /* the inputted number to process */
  7. int right_digit; /* right most digit of a number */
  8. int sum_of_digits; /* the sum of the digits */
  9.  
  10. /* initialize sum of digits to zero */
  11. sum_of_digits = 0;
  12.  
  13. /* prompt for number to process */
  14. printf("Enter your number: ");
  15. scanf("%d", &number);
  16. printf("\n");
  17.  
  18. /* loop to strip out each of the digits */
  19. while (number != 0)
  20. {
  21. right_digit = number % 10; /* get the right most digit */
  22. printf("right digit = %d", right_digit);
  23.  
  24. /* add right most digit to our running total */
  25. sum_of_digits += right_digit;
  26. number = number / 10; /* or number /* 10; */
  27. printf(", number = %d\n", number);
  28. }
  29.  
  30. /* output the sum of the digits */
  31. printf("\nSum_of_digits = %d\n", sum_of_digits);
  32.  
  33. return (0);
  34. }
Success #stdin #stdout 0s 5288KB
stdin
8947
stdout
Enter your number: 
right digit = 7, number = 894
right digit = 4, number = 89
right digit = 9, number = 8
right digit = 8, number = 0

Sum_of_digits = 28