Reverse a number without using loop in python

Python Program to Reverse a Number Using While Loop

  • This program allows the user to enter any positive integer and then, this program will reverse a number using Python While Loop

Reverse a number without using loop in python

Learn Python - Python tutorial - Python Program to Reverse a Number - Python examples - Python programs

Sample Code

# Python Program to Reverse a Number using While loop
 
Number = int(input("Please Enter any Number: "))
Reverse = 0
while(Number > 0):
    Reminder = Number %10
    Reverse = (Reverse *10) + Reminder
    Number = Number //10
 
print("\n Reverse of entered number is = %d" %Reverse)

OUTPUT

Reverse a number without using loop in python

Learn Python - Python tutorial - Python Program to Reverse a Number Using While Loop - Python examples - Python programs

Analysis:

  • This program allows the user to enter any positive integer and then, that number is assigned to variable Number.
  • Next, Condition in the While loop will make sure that, the given number is greater than 0
  • From the above example, User Entered value: Number = 1456 and Reverse = 0

First Iteration

Reminder = Number %10
Reminder = 1456%10 = 6
Reverse = Reverse *10+ Reminder
Reverse = 0 * 10 + 6
Reverse = 0 + 6
Reverse = 6
Number = Number //10
Number = 1456 //10
Number = 145

Second Iteration

From the first Iteration the values of both Number and Reverse has been changed as:
Number = 145 and Reverse = 6
Reminder = Number % 10
Reminder = 145 % 10 = 5
Reverse = Reverse *10+ Reminder
Reverse = 6 * 10 + 5
Reverse = 60 + 5
Reverse = 65
Number = Number //10
Number = 145 //10
Number = 14

Third Iteration

From the Second Iteration the values of both Number and Reverse has been changed as:
Number = 14 and Reverse = 65
Reminder = Number %10
Reminder = 14%10 = 4
Reverse = Reverse *10+ Reminder
Reverse = 65 * 10 + 4
Reverse = 650 + 4
Reverse = 654
Number = Number //10
Number = 14//10
Number = 1

Fourth Iteration

From the Second Iteration the values of both Number and Reverse has been changed as:
Number = 1 and Reverse = 654
Reminder = Number %10
Reminder = 1 %10 = 1
Reverse = Reverse *10+ Reminder
Reverse = 654 * 10 + 1
Reverse = 6540 + 1
Reverse = 6541
Number = Number //10
Number = 1//10
Number = 0

  • Here, For the next iteration Number = 0 so, the while loop condition will fail

Python Program to Reverse a Number Using Functions

  • This program allows the user to enter any positive integer and then, we are going to reverse a number using Python Functions

Sample Code

# Python Program to Reverse a Number using Functions
 
def Reverse_Integer(Number):
    Reverse = 0
    while(Number > 0):
        Reminder = Number %10
        Reverse = (Reverse *10) + Reminder
        Number = Number //10
    return Reverse
 
Number = int(input("Please Enter any Number: "))
Reverse = Reverse_Integer(Number)
print("\n Reverse of entered number is = %d" %Reverse)

OUTPUT

Reverse a number without using loop in python

Learn Python - Python tutorial - Python Program to Reverse a Number Using Functions - Python examples - Python programs

ANALYSIS:

  • When the compiler reaches to Reverse = Reverse_Integer (Number) line in the program then the compiler will immediately jump to below function:

def Reverse_Integer(Number):

  • We already explained the code LOGIC in the above example. Please refer Python Program to Reverse an Integer Using While Loop Analysis.
  • Last line ends with return Reverse statement.

Python Program to Reverse a Number using Recursion

  • This program allows the user to enter any positive integer and then, we are going to reverse a number using Python Recursion

Sample Code

# Python Program to Reverse a Number using Recursion
 
Reverse = 0
def Reverse_Integer(Number):
    global Reverse
    if(Number > 0):
        Reminder = Number %10
        Reverse = (Reverse *10) + Reminder
        Reverse_Integer(Number //10)
    return Reverse
 
Number = int(input("Please Enter any Number: "))
Reverse = Reverse_Integer(Number)
print("\n Reverse of entered number is = %d" %Reverse)

OUTPUT

Reverse a number without using loop in python

Learn Python - Python tutorial - Python Program to Reverse a Number using Recursion - Python examples - Python programs

ANALYSIS:

  • When the compiler reaches to Reverse = Reverse_Integer (Number) line in the program then the compiler will immediately jump to below function:

def Reverse_Integer(Number):

  • In this function, below statement will help to call the function Recursively with updated value.
  • If you miss this statement then, after completing the first line it will terminate.

Reverse_Integer(Number //10)

  • For example, Number = 459 will produce the output as 9
  • Lets see the Python If Statement, if (Number > 0) will check whether the number is greater than 0 or not. For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).

How do you reverse a number without a loop?

We will use two variable for reverse the numbers. Just simple mathematic operation we will perform to reverse the number..
First initialize two integer variable like (num, temp)..
Now perform reverse operation with both variable..
Finally show the result on output screen after reverse..

How do I reverse a number in Python?

Algorithm.
Input Integer: number..
(1) Initialize variable revs_number = 0..
(2) Loop while number > 0..
(a) Multiply revs_number by 10 and add remainder of number..
divide by 10 to revs_number..
revs_number = revs_number*10 + number%10;.
(b) Divide num by 10..
(3) Return revs_number..

How do you reverse a number in a for loop in Python?

In this program, while loop is used to reverse a number as given in the following steps: First, the remainder of the num divided by 10 is stored in the variable digit . Now, the digit contains the last digit of num , i.e. 4. digit is then added to the variable reversed after multiplying it by 10.

How do you reverse a number using slicing in Python?

Example 1: Reverse Number using String slicing If the given input is not a number, we shall print a message to the user. try: n = int(input('Enter a number : ')) reversed = int(str(n)[::-1]) print(reversed) except ValueError: print('Given input is not a number. ')