207k views
11 votes
(Reverse number) Write a program that prompts the user to enter a four-digit inte- ger and displays the number in reverse order. Here is a sample run:

User Radu Szasz
by
4.6k points

1 Answer

13 votes

Answer:

The program in Python is as follows:

num = int(input("4 digit number: "))

numstr = str(num)

if(len(numstr)!=4):

print("Number must be 4 digits")

else:

numstr = numstr[::-1]

num = int(numstr)

print(num)

Step-by-step explanation:

This prompts user for input

num = int(input("4 digit number: "))

This converts the input number to string

numstr = str(num)

This checks if number of digits is or not 4

if(len(numstr)!=4):

If not, the following message is printed

print("Number must be 4 digits")

else:

If digits is 4,

This reverses the string

numstr = numstr[::-1]

This converts the reversed string to integer

num = int(numstr)

This prints the reversed number

print(num)

User Nicola Uetz
by
5.2k points