Python program to find reverse of a number

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to find reverse of a number.

Code for python program to find reverse of a number

def reverse_number(num):
    reverse = 0
    while num > 0:
        rem = num % 10
        reverse = (reverse * 10) + rem
        num = num // 10
    return reverse

n = int(input("Enter a number: "))
print("Reverse of the number", n, "is", reverse_number(n))

Output

Enter a number: 12
Reverse of the number 12 is 21

Explanation

In this program, the reverse_number(num) function takes one argument: a number. The function uses a while loop to repeatedly extract the last digit of the number and add it to a new number with digits shifted to the left. At the end of the loop, the function returns the reverse of the given number.

The program starts by asking the user to enter the number. This value is stored in the variable n. The function reverse_number is called with n as an argument. The returned value of the function is printed with message “Reverse of the number n is”.

This program uses modulus(%) and floor division(//) operator to extract the last digit and removing it from the number respectively.

Python program to find the factorial of n numbers

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment