Write A Python Script To Read An Integer 1000 And Reverse The Number

Hi in this page you will learn a python script to reverse a given integer and display it on to the standard output.

Let's ask the user to enter an integer number and then convert it  using the type conversion and then calculate the reverse of a number using a while loop and then print  it on to the standard output.

Let's try to do this using the python script.

 

Write A Python Script To Read An Integer 1000 And Reverse The Number

 

 


Write A Python Script To Read An Integer 1000 And Reverse The Number

Take a variable named as number and use the input function call to ask the user to enter a number and then convert it into an integer using the type conversion.

Take another variable name as reverse and initialize it to zero. The reverse variable will be used to store the reverse of a given input number.

number = int(input("Enter a number : "))
reverse = 0




Use the while loop for the purpose of iteration,  the condition for the while loop is until the number becomes 0 so you have to perform the iterative steps.

Try to find the last digit of a given number by modulating it with the 10, multiply the reverse of a number by 10 and the last digit. In order to take out the last digit from a number, perform true division by 10.

while number != 0:
   last_digit = number % 10
   reverse = reverse * 10 + last_digit
   number = number // 10




Once the while you complete the reverse variable will store the reverse of a number. Use the print function call to print the reverse variable to standard output.

print("Reverse of Number is :", reverse)


Write A Python Script To Read An Integer 1000 And Reverse The Number




Complete Python Program
===============================

number = int(input("Enter a number : "))
reverse = 0

while number != 0:
   last_digit = number % 10
   reverse = reverse * 10 + last_digit
   number = number // 10

print("Reverse of Number is :", reverse)





Output
===============

Enter a number : 1000
Reverse of Number is : 1




Output 2
===============

Enter a number : 1234
Reverse of Number is : 4321



Output 3
=============

Enter a number : 88330
Reverse of Number is : 3388




Conclusion
=================

The above python script uses a while loop to  calculate the reverse of a given integer number.

Comment it down below if you have any queries related to about Python program

 



Post a Comment

0 Comments