92. Python Program to Find the Sum of Digits of a Number
Mediumwhile
Overview
A loop that runs until a goal is reached: keep taking digits off the number until nothing is left.
Problem Statement
Read a whole number (0 or more) and print the sum of its digits. For example, for 4728 the sum is 4 + 7 + 2 + 8:
Sum of digits: 21
Use a while loop. n % 10 gives the last digit, and n // 10 removes it. For 0, the sum is 0.
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
One value per line. Example:
4728
Output
The output for that example:
Sum of digits: 21
The tests try 5 different inputs.