27.9k views
5 votes
Write a program with class name Digits that prompts the user to input a positive integer and then outputs the number reversed and the sum of the digits. For example, if the user enters the number 3456, then your program should output 6543 and the sum as 18. Use a while loop. Hint: use the mod operator and 10 as divider to find the right most digit and to update the value of controlling expression

User Mxyk
by
4.9k points

1 Answer

7 votes

Answer:

Written in Python:

inputnum = int(input("User Input: "))

outputnum = 0

total = 0

while(inputnum>0):

remainder = inputnum % 10

outputnum = (outputnum * 10) + remainder

inputnum = inputnum//10

total = total + remainder

print("Reverse: "+str(outputnum))

print("Total: "+str(total))

Step-by-step explanation:

This prompts user for input

inputnum = int(input("User Input: "))

This initializes the reverse number to 0

outputnum = 0

This initializes total to 0; i.e. sum of each digit

total = 0

The following iteration gets the reverse of user input

while(inputnum>0):

remainder = inputnum % 10

outputnum = (outputnum * 10) + remainder

inputnum = inputnum//10

This adds each digit of user input

total = total + remainder

This prints the reversed number

print("Reverse: "+str(outputnum))

This prints the sum of each digit

print("Total: "+str(total))

User Gofvonx
by
4.7k points