SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
Functions Strings And Objects
Exercise:
Programming Excercises
Question:11 | ISBN:978013274719 | Edition: 6

Question

(Reverse number) Write a program that prompts the user to enter a four-digit integer and displays the number in reverse order. Here is a sample run: 

Enter an integer: 3125
The reversed number is 5213

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Reverse Number Program Code:


# Prompt the user to enter a four-digit integer 
number = int(input("Enter an integer: "))

# Extract each digit from the number
digitOne = number % 10
digitTwo = (number // 10) % 10
digitThree = (number // 100) % 10
digitFour = (number // 1000) % 10

# Build the reversed number
reversedNumber = digitOne * 1000 + digitTwo * 100 + digitThree * 10 + digitFour

# Display the reversed number
print("The reversed number is", reversedNumber)

Executed Output 1:

Enter an integer: 3125
The reversed number is 5213

Executed Output 2:

Enter an integer: 1234
The reversed number is 4321

 

0 0

Discussions

Post the discussion to improve the above solution.