write a program to reverse a number in python

In this article  let's learn how to write a program to reverse a number in python. 

 

write a program to reverse a number in python



In order to reverse a number you have to follow a technique that is modulus addition and division (MAD) by 10.  Initialize A variable name the reverse to 0, given an input number, get the last digit by performing modulus by 10.  Then  multiply 10 bi reverse number and at the last digit to the rivers.  Trim the last digit of a number by performing true division by 10.



Python program to reverse a number using while loop

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 a Number is : ", reverse)




OUTPUT:

Enter a Number : 987654321
Reverse of a Number is :  123456789



Conclusion:  Try to to run the program by yourself by entering a different number and see the difference let me know if any queries in this problem   by commenting down below



Post a Comment

0 Comments