Write a Python Code to Reverse The Given Integer And Print The Integer

Let's ask a user to enter an integer  using the python input function call,  then  reverse the  integer and print the reverse of an integer as output.

We will be using while loop to solve this python program.

 

Write a Python Code to Reverse The Given Integer And Print The Integer

 



Write a Python Code to Reverse The Given Integer And Print The Integer

Let's take a variable name 10 number and ask the user to enter the number using the input function call and then convert it into an integer using the type conversion.  Once the user enters a number, copy the contents of the number into another temporary variable named temp.  take another variable named reverse_number to store the reverse of a number.

number = int(input("Enter a Number : "))
temp = number
reverse_number = 0





Using the while loop completes  until the value of temp (i.e, number)  becomes zero,  try modulus, addition and division (MAD) on the temp value. Take a local variable named last_digit and get the last digit by modulating it with 10.  add the last_digit to the reverse of a number,  after adding trim the last digit of temp by performing true division by 10.

Use print() function to print the reverse_number.

while temp != 0:
   last_digit = temp % 10
   reverse_number = reverse_number * 10 + last_digit
   temp = temp // 10

print(f"Reverse on {number} is {reverse_number}")




Code
==============

number = int(input("Enter a Number : "))
temp = number
reverse_number = 0

while temp != 0:
   last_digit = temp % 10
   reverse_number = reverse_number * 10 + last_digit
   temp = temp // 10

print(f"Reverse on {number} is {reverse_number}")





Output
=================
Enter a Number : 12345
Reverse on 12345 is 54321





Output
==============
Enter a Number : 4560789
Reverse on 4560789 is 9870654




Conclusion
==================
Execute the above Python program by providing an integer value as an input to it, note down the results.

Comment down below if you have any suggestions to improve the Python program


Post a Comment

0 Comments