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
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
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