34.1k views
5 votes
Write a program (wap) that will reverse the digits of an input infeger.

sample input 13579 4321
sample output 97531 1234

1 Answer

2 votes

Final answer:

You can reverse the digits of an input integer by using a loop to extract the last digit and rebuilding the number in reverse order.

Step-by-step explanation:

To reverse the digits of an input integer, you can use a loop to extract the last digit of the number and then multiply the previously reversed number by 10 and add the extracted digit. Here's a program in Python that accomplishes this:

num = int(input('Enter an integer: '))
reversed_num = 0

while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num = num // 10

print('Reversed number:', reversed_num)

In this program, we use the modulus operator (%) to extract the last digit from the integer. We then use the integer division operator (//) to remove the extracted digit from the original number. The reversed number is built by multiplying the previously reversed number by 10 and adding the extracted digit.

User Totymedli
by
8.3k points