Reverse a Number using Python

 

Topic: Reverse a Number using Python.

Hey Guys, today we are going see a Python Program to reverse a number using user input.

Title: Python Code for Reverse a Number with user input. (Beginner Friendly + Easy + Code + Output + Online Compiler)


What is this program and why we are going to use it?

This Program consists of Variables, Input Function, Loop (while loop) and Arithmetic Operators.

By using variables, we are going to store values in variables in int format. By using input function, we are going to input values from the user. For example:
n = 1234
Here n is a variable where 1234 is assigned to n.

Now for input function

n = int(input("Enter a number: "))
So, this will ask user to input a number.

Logic:

1. Take a number (n) from user.
2. Initialize reverse = 0.
3. Use a while loop until n > 0.
   - Extract last digit using digit = n % 10
   - Add digit to reverse using reverse = reverse * 10 + digit
   - Remove last digit using n = n // 10
4. Display the reversed number.

Code:


Online Compiler (Switch to Interactive Mode before execution):



Example Output:

Input: 1234
Output: 4321

Applications:

1. Used in palindrome number checking
2. Helps in understanding loops and number manipulation
3. Useful in algorithm and logic building

Comments