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

Sum_of_digits = 17