n = int(input())
original = n
count = total = 0
while n > 0:
total += n % 10 # last digit
n //= 10 # drop the last digit
count += 1
print(f"{original} has {count} digits, sum = {total}")
Output Verified by running it on Python 3
987654321 has 9 digits, sum = 45
n % 10 extracts the last digit and n //= 10 removes it, until nothing is left.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!