Sum of Digits, Reduced to a Single Digit
MediumRead a positive integer and repeatedly replace it by the sum of its digits until a single digit remains, printing every stage on one line separated by a space, a hyphen, a greater-than sign, and a space: 9875 prints 9875 -> 29 -> 11 -> 2. A number that already has one digit prints just itself. Use a nested loop: the outer loop runs while the number has more than one digit, and the inner loop peels the digits with % 10 and / 10. If the number is below 1, print N must be positive and return 1. If it cannot be read, print invalid input and return 1.
Success Criteria
Your code must pass 8 test case(s) to complete this exercise. 3 hint(s) are available if you need help.
Sign in to track your progress
You can work on exercises as a guest, but sign in to track your progress and save your submissions.
How did you find this exercise?
Your rating helps us improve the content.