PPA-7
Question
Accept a five digit number as input and print the sum of its digits as output.
Hint
Sometimes, it may be more beneficial to accept a string as input and not convert it into an integer right away.
What do you think the variable first
contains in the above snippet of code? Do you see how you can extend this idea to solve the problem?
As a second approach, we can start with \(x\) as an integer. Instead of processing the number from left to right, let us try to process it from right to left. How do we get the last digit of a number? The last digit is the remainder when the number is divided by \(10\):
Once we have the last digit, we no longer need it. So, we need to remove it from \(x\). How do we do it? We can do floor division by \(10\):
Can you complete the solution?