Loop

Python program to reverse the given number

Python program to reverse the given number

In this article, we will discuss the concept of the Python program to reverse the given number

In this post, we are going to learn how to find reverse of the given number in Python programming language

Program to reversed the given number

Code to reverse a number using the loop

The program allows the user to enter a number and then it displays the reverse number of the given number using while loop in Python language

Program 1

#Pyton program to reverse a number using while loop
Number=int(input("Pleaae enter a number for reverse: ")) #Ask input from the user
print(" Entered number is: ",Number);
Reverse=0;#declare and initialise variable
while(Number>0):
    Reminder=Number %10
    Reverse=(Reverse *10)+Reminder
    Number=Number//10;

    
print(" Reversed number is %d: "%Reverse);
    

When the above code is executed, it produces the following result

Pleaae enter a number for reverse: 54321
(' Entered number is: ', 54321)
 Reversed number is 12345:

 

Code to reverse a number using the function

The program allows the user to enter a number and then it displays the reverse number of given number using the function in Python language

Program 1

#Python program to reverse a number using function
def reverse_Num(Number):  #Define a function
    Reverse=0             #declarae and initialize variable Reserve
    while(Number>0):
        Reminder=Number %10
        Reverse=(Reverse *10)+Reminder
        Number=Number//10;
    return Reverse    #return reverse value to function
Number=int(input("Please enter a number: "))
Reverse=reverse_Num(Number)#Call the function

print("The entered number is: %d" %Number)
print("The reverse number is: %d" %Reverse)

When the above code is executed, it produces the following result

Please enter a number: 34567
The entered number is: 34567
The reverse number is: 76543

 

Code to reverse a number using recursion

The program allows the user to enter a number and then it displays the reverse number of the given number using the recursion in Python language

Program 3

#Python program to reverse a number using recursion

Reverse=0  #global variable
def reverse_Num(Number):  #Define a function
    global Reverse;          
    if(Number>0):
        Reminder=Number %10
        Reverse=(Reverse *10)+Reminder
        reverse_Num(Number//10);
    return Reverse    #return reverse value to function
Number=int(input("Please enter a number: "))
Reverse=reverse_Num(Number)#Call the function

print("The Entered number is: %d" %Number)
print("The reverse number is: %d" %Reverse)

When the above code is executed, it produces the following result

Please enter a number: 23456
The entered number is: 23456
The reverse number is: 65432

 

Suggested for you

For loop in python language

While loop in Python language

Function in Python language

Recursion in Python language

 

Similar post

Java program to reverse the given number using loops

C++ program to reverse the given number using loops

C program to reverse the given number using loops

 

Java program to reverse the given String using loops

C++ program to reverse the given String using loops

C program to reverse the given String using loops

 

C++ Program for Display Alphabets using ASCII value
Rhombus and Hollow Rhombus star pattern in Java using for loop
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

3 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago