Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Reverse a Number in Python

How to Reverse a Number in Python

Posted on January 28, 2025January 28, 2025 By Rishabh saini No Comments on How to Reverse a Number in Python

Reverse a Number in Python

Reversing a number is one of the most frequently asked programming questions in interviews. Python offers multiple methods to accomplish this, each with its own learning opportunities. In this post, we will explore two primary approaches: using a while loop and using recursion.

Complete Python Course with Advance topics:-Click here

Reversing a Number in Python

We will create programs that take an integer input from the user and reverse the number. Here are two methods to achieve this:

  1. Using a While Loop
  2. Using Recursion

Method 1: Reverse a Number Using a While Loop

Before diving into the code, let’s break down the logic in a step-by-step algorithm.

Algorithm

Input: An integer, number

  1. Initialize a variable reversed_number = 0.
  2. While the number > 0:
    • Extract the remainder of the number when divided by 10.
    • Multiply reversed_number by 10, then add the remainder.
    • Divide number by 10 (integer division).
  3. Return the value of reversed_number.

Code Implementation

# Program to reverse a number using a while loop

# Take user input
number = int(input("Enter an integer number: "))  

# Initialize reversed_number to 0
reversed_number = 0  

# Loop to reverse the number
while number > 0:  
    remainder = number % 10  # Extract the last digit
    reversed_number = (reversed_number * 10) + remainder  # Build the reversed number
    number = number // 10  # Remove the last digit from the number

# Display the reversed number
print(f"The reversed number is: {reversed_number}")  

Output

Enter an integer number: 12345  
The reversed number is: 54321  

Reverse a Number in Python

Explanation of the While Loop Program

Let’s break down the code step by step with an example input of 12345:

  1. First Iteration:
    • remainder = 12345 % 10 = 5
    • reversed_number = 0 * 10 + 5 = 5
    • number = 12345 // 10 = 1234
  2. Second Iteration:
    • remainder = 1234 % 10 = 4
    • reversed_number = 5 * 10 + 4 = 54
    • number = 1234 // 10 = 123
  3. Third Iteration:
    • remainder = 123 % 10 = 3
    • reversed_number = 54 * 10 + 3 = 543
    • number = 123 // 10 = 12
  4. Fourth Iteration:
    • remainder = 12 % 10 = 2
    • reversed_number = 543 * 10 + 2 = 5432
    • number = 12 // 10 = 1
  5. Fifth Iteration:
    • remainder = 1 % 10 = 1
    • reversed_number = 5432 * 10 + 1 = 54321
    • number = 1 // 10 = 0

The loop terminates as number becomes 0, and the reversed number 54321 is printed.

Method 2: Reverse a Number Using Recursion

Recursion is another effective way to reverse a number. Let’s look at the logic and implementation.

Algorithm

Input: An integer number

  1. Create a global variable reversed_number = 0.
  2. If number > 0:
    • Extract the remainder of the number when divided by 10.
    • Multiply reversed_number by 10 and add the remainder.
    • Call the function recursively, passing number // 10.
  3. Return reversed_number.

Code Implementation

# Program to reverse a number using recursion

# Take user input
number = int(input("Enter the number: "))  
reversed_number = 0  # Initialize reversed_number

# Define the recursive function
def reverse_number(num):  
    global reversed_number  
    if num > 0:  
        remainder = num % 10  # Extract the last digit
        reversed_number = (reversed_number * 10) + remainder  # Build the reversed number
        reverse_number(num // 10)  # Recursive call
    return reversed_number  

# Call the function and print the result
result = reverse_number(number)  
print(f"The reversed number is: {result}")  

Output

Enter the number: 5426  
The reversed number is: 6245  

Reverse a Number in Python

Key Takeaways

  • Both methods, while loop and recursion, use the same logic to reverse a number.
  • While the loop method is iterative and straightforward, recursion provides an elegant and compact solution.
  • Understanding the logic behind reversing a number will allow you to write similar programs in other languages like Java, C++, or JavaScript.

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE


reverse a number in python using for loop
reverse a number in python w3schools
reverse of a number in python using function
how to reverse a number in java
reverse a number in python using while loop
reverse a number in python using slicing
reverse a number in python without using loop
python program to reverse a string
online python compiler
online c compiler
how to reverse a number in python using for loop
how to reverse a number in python for loop

Post Views: 579
Python Interview Question Tags:how to reverse a number in python, Python, python program to find the reverse of a number, python program to reverse a number, python reverse a number, python reverse number, reverse a number, reverse a number in python, reverse number in python, reverse of a given number, reverse of a given number in python, reverse of a number in python, reverse of a number in python using while loop, reverse of a number python program

Post navigation

Previous Post: YouTube Video Downloader Using Python and Django
Next Post: SQL SELECT DATE: Retrieving and Filtering Dates Professionally

More Related Articles

for Loop in Python How to Use for Loop in Python: A Comprehensive Guide Python Interview Question
How to Convert Integer to String in Python: A Comprehensive Guide - How to Convert Integer to String in Python How to Convert Integer to String in Python: A Comprehensive Guide Python Interview Question
How to Create a DataFrame in Python How to Create a DataFrame in Python Python Interview Question

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. How to Install Python on Windows: A Step-by-Step Guide
  2. How to Run Python Program: A Comprehensive Guide for Python Programmers
  3. How to Append Elements to a List in Python
  4. How to Declare a Global Variable in Python
  5. How to Create a DataFrame in Python
  6. Insertion Sort in Python

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,616)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,227)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,877)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme