python program for reverse of a number using type casting
In this tutorial, we are going to learn how to reverse a given number within two lines of code in Python using the type casting method.
How to reverse a number (optimum solution) in Python
Before jump into the program 1st, you have some knowledge of typecasting in Python.
following are the way of typecasting in python:
let’s consider two variable
x=’123′ (string
variable)
y=123 (integer variable)
1)integer to string:
—-> z=str(y)
—-> o/p= ‘123’
2)string to an integer:
—-> z=int(x)
—-> o/p=123
3)integer to float:
—-> z=float(y)
—-> o/p=123.0
In similar way in this program we are going to use type casting .
Now move on the program.
first, take a value from the user:
#take integer as a string input from users.
x=input("Please enter a integer ")
Now use type cast concept to convert a string because the reverse of
number is easier than the reverse of the string so we directly taking a number as a string then reverse them.
the reverse of given number which is taken by the user in a string format.
#reverse of string instead of integer.
y=x[::-1]
Now again typecast the string into integer and print it as an output.
#type cast from string to integer.
y=int(y)
print("Reverse of given number = ",y)
let’s combine the whole code together :
# Reverse of a number in python within very less line of code.
#take integer as a string input from users.
x=input("Please enter a integer ")
#reverse of string instead of integer.
y=x[::-1]
#type cast from string to integer.
y=int(y)
print("Reverse of given number = ",y)
Output:
Please enter a integer 156
Reverse of given number = 651
You may also read:
- Python Program to reverse digits of a given number
Write a program to reverse the digits of an integer.
Examples :
Input : num = 12345
Output: 54321
Input : num = 876
Output: 678
Flowchart:
ITERATIVE WAY
Algorithm:
Input: num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num
Example:
num = 4562
rev_num = 0
rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456
rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45
rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4
rev_num = rev_num *10 + num%10 = 2650 + 4 = 2654
num = num/10 = 0
Program:
C++
#include
using
namespace
std;
int
reverseDigits(
int
num)
{
int
rev_num = 0;
while
(num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return
rev_num;
}
int
main()
{
int
num = 4562;
cout <<
"Reverse of no. is "
<< reverseDigits(num);
getchar
();
return
0;
}
C
#include
int
reverseDigits(
int
num)
{
int
rev_num = 0;
while
(num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return
rev_num;
}
int
main()
{
int
num = 4562;
printf
(
"Reverse of no. is %d"
, reverseDigits(num));
getchar
();
return
0;
}
Java
class
GFG {
static
int
reverseDigits(
int
num)
{
int
rev_num =
0
;
while
(num >
0
) {
rev_num = rev_num *
10
+ num %
10
;
num = num /
10
;
}
return
rev_num;
}
public
static
void
main(String[] args)
{
int
num =
4562
;
System.out.println(
"Reverse of no. is "
+ reverseDigits(num));
}
}
Python
n
=
4562
rev
=
0
while
(n >
0
):
a
=
n
%
10
rev
=
rev
*
10
+
a
n
=
n
/
/
10
print
(rev)
C#
using
System;
class
GFG {
static
int
reverseDigits(
int
num)
{
int
rev_num = 0;
while
(num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return
rev_num;
}
public
static
void
Main()
{
int
num = 4562;
Console.Write(
"Reverse of no. is "
+ reverseDigits(num));
}
}
PHP
function
reverseDigits(
$num
)
{
$rev_num
= 0;
while
(
$num
> 1)
{
$rev_num
=
$rev_num
* 10 +
$num
% 10;
$num
= (int)
$num
/ 10;
}
return
$rev_num
;
}
$num
= 4562;
echo
"Reverse of no. is "
,
reverseDigits(
$num
);
?>
Javascript
let num = 4562;
function
reverseDigits(num) {
let rev_num = 0;
while
(num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = Math.floor(num / 10);
}
return
rev_num;
}
document.write(reverseDigits(num));
Output
Reverse of no. is 2654
Time Complexity: O(n), where
n is the input number.
Auxiliary Space: O(1)
RECURSIVE WAY :
C++
#include
using
namespace
std;
int
reverseDigits(
int
num)
{
static
int
rev_num = 0;
static
int
base_pos = 1;
if
(num > 0) {
reverseDigits(num / 10);
rev_num += (num % 10) * base_pos;
base_pos *= 10;
}
return
rev_num;
}
int
main()
{
int
num = 4562;
cout <<
"Reverse of no. is "
<< reverseDigits(num);
return
0;
}
C
#include ;
int
reversDigits(
int
num)
{
static
int
rev_num = 0;
static
int
base_pos = 1;
if
(num > 0) {
reversDigits(num / 10);
rev_num += (num % 10) * base_pos;
base_pos *= 10;
}
return
rev_num;
}
int
main()
{
int
num = 4562;
printf
(
"Reverse of no. is %d"
, reversDigits(num));
getchar
();
return
0;
}
Java
class
GFG {
static
int
rev_num =
0
;
static
int
base_pos =
1
;
static
int
reversDigits(
int
num)
{
if
(num >
0
) {
reversDigits(num /
10
);
rev_num += (num %
10
) * base_pos;
base_pos *=
10
;
}
return
rev_num;
}
public
static
void
main(String[] args)
{
int
num =
4562
;
System.out.println(reversDigits(num));
}
}
Python3
rev_num
=
0
base_pos
=
1
def
reversDigits(num):
global
rev_num
global
base_pos
if
(num >
0
):
reversDigits((
int
)(num
/
10
))
rev_num
+
=
(num
%
10
)
*
base_pos
base_pos
*
=
10
return
rev_num
num
=
4562
print
(
"Reverse of no. is "
,
reversDigits(num))
C#
using
System;
class
GFG {
static
int
rev_num = 0;
static
int
base_pos = 1;
static
int
reversDigits(
int
num)
{
if
(num > 0) {
reversDigits(num / 10);
rev_num += (num % 10) * base_pos;
base_pos *= 10;
}
return
rev_num;
}
public
static
void
Main()
{
int
num = 4562;
Console.WriteLine(reversDigits(num));
}
}
PHP
$rev_num
= 0;
$base_pos
= 1;
function
reversDigits(
$num
)
{
global
$rev_num
;
global
$base_pos
;
if
(
$num
> 0)
{
reversDigits((int)(
$num
/ 10));
$rev_num
+= (
$num
% 10) *
$base_pos
;
$base_pos
*= 10;
}
return
$rev_num
;
}
$num
= 4562;
echo
"Reverse of no. is "
,
reversDigits(
$num
);
?>
Javascript
var
rev_num = 0;
var
base_pos = 1;
function
reversDigits(num)
{
if
(num > 0)
{
reversDigits(Math.floor(num/10));
rev_num += (num%10)*base_pos;
base_pos *= 10;
}
return
rev_num;
}
let num = 4562;
document.write(
"Reverse of no. is "
+ reversDigits(num));
Time Complexity: O(log(n))
Auxiliary Space: O(log(n)), where n is the input number.
Using Recursion, without extra variable
C++
#include
#include
using
namespace
std;
int
len(
int
number)
{
return
log10
(number)+1;
}
int
rev_number(
int
& number)
{
if
( (number % 10) == number )
return
number;
int
last = number % 10;
int
remaining = number / 10;
int
l = len(remaining);
return
last*
pow
(10, l) + rev_number(remaining);
}
int
main()
{
int
number = 123456;
cout << rev_number(number) << endl;
return
0;
}
Java
import
java.lang.Math;
import
java.util.*;
class
GFG {
public
static
int
len(
int
number)
{
return
(
int
)(Math.log10(number)) +
1
;
}
public
static
int
rev_number(
int
number)
{
if
(number %
10
== number) {
return
number;
}
int
last = number %
10
;
int
remaining = number /
10
;
int
l = len(remaining);
return
last * (
int
)Math.pow(
10
, l)
+ rev_number(remaining);
}
public
static
void
main(String[] args)
{
int
number =
123456
;
System.out.println(rev_number(number));
}
}
Python3
def
number_length(num):
return
len
(
str
(num))
def
rev_number(number):
if
(number
%
10
)
=
=
number:
return
number
last
=
number
%
10
remaining
=
number
/
/
10
l
=
number_length(remaining)
return
last
*
pow
(
10
, l)
+
rev_number(remaining)
def
main():
number
=
123456
print
(rev_number(number))
if
__name__
=
=
"__main__"
:
main()
Using String in java
We will convert the number to a string using StringBuffer after this, we will reverse that string using the reverse() method
corner case
Input: 32100
So for the above input if we try to solve this by reversing the string, then the output will be 00123.
So to deal with this situation we again need to convert the string to integer so that our output will be 123
C++
#include
using
namespace
std;
int
reverseDigits(
int
num)
{
string strin = to_string(num);
reverse(strin.begin(), strin.end());
num = stoi(strin);
return
num;
}
int
main()
{
int
num = 4562;
cout <<
"Reverse of no. is "
<< reverseDigits(num);
return
0;
}
Java
public
class
GFG {
static
int
reversDigits(
int
num)
{
StringBuffer string
=
new
StringBuffer(String.valueOf(num));
string.reverse();
num = Integer.parseInt(String.valueOf(string));
return
num;
}
public
static
void
main(String[] args)
{
int
num =
4562
;
System.out.println(
"Reverse of no. is "
+ reversDigits(num));
}
}
Python3
def
reversDigits(num):
string
=
str
(num)
string
=
list
(string)
string.reverse()
string
=
''.join(string)
num
=
int
(string)
return
num
if
__name__
=
=
"__main__"
:
num
=
4562
print
(
"Reverse of no. is "
, reversDigits(num))
C#
using
System;
public
class
GFG{
public
static
string
ReverseString(
string
s)
{
char
[] array = s.ToCharArray();
Array.Reverse(array);
return
new
string
(array);
}
static
int
reversDigits(
int
num)
{
string
strin = num.ToString();
strin = ReverseString(strin);
num =
int
.Parse(strin);
return
num;
}
static
public
void
Main ()
{
int
num = 4562;
Console.Write(
"Reverse of no. is "
+ reversDigits(num));
}
}
Javascript
function
reversDigits(num)
{
let str
= num.toString().split(
""
).reverse().join(
""
);
num = parseInt(str);
return
str;
}
let num = 4562;
document.write(
"Reverse of no. is "
+ reversDigits(num));
Time Complexity: O(log10n) where n is the input number
Auxiliary Space: O(1)
Reverse digits of an integer with overflow handled
Note that the above program doesn’t consider leading zeroes. For example, for 100 programs will print 1. If you want to print 001 then see this comment from Maheshwar.
Using Slicing in Python:
C++
#include
using
namespace
std;
int
reversDigits(
int
num){
string str = to_string(num);
reverse(str.begin(), str.end());
num = stoll(str);
return
num;
}
int
main()
{
int
num = 4562;
cout <<
"Reverse of no. is "
<< reversDigits(num) ;
return
0;
}
Python3
def
reversDigits(num):
string
=
str
(num)
string
=
string[::
-
1
]
num
=
int
(string)
return
num
if
__name__
=
=
"__main__"
:
num
=
4562
print
(
"Reverse of no. is "
, reversDigits(num))
C#
using
System;
class
GFG {
static
int
reversDigits(
int
num)
{
string
str = Convert.ToString(num);
char
[] charArray = str.ToCharArray();
Array.Reverse(charArray);
str =
new
string
(charArray);
num = Convert.ToInt32(str);
return
num;
}
public
static
void
Main(
string
[] args)
{
int
num = 4562;
Console.Write(
"Reverse of no. is "
+ reversDigits(num));
}
}
Javascript
function
reversDigits(num){
let str = num.toString().split(
""
);
str.reverse();
num = parseInt(str.join(
""
))
return
num;
}
let num = 4562;
console.log(
"Reverse of no. is "
+ reversDigits(num));
Java
import
java.util.*;
class
GFG {
static
int
reversDigits(
int
num)
{
String str = String.valueOf(num);
str =
new
StringBuilder(str).reverse().toString();
num = Integer.valueOf(str);
return
num;
}
public
static
void
main(String[] args)
{
int
num =
4562
;
System.out.println(
"Reverse of no. is "
+ reversDigits(num));
}
}
Time Complexity: O(n), where n is the input number
Auxiliary Space: O(1)
Try extensions of above functions that should also work for floating-point numbers.
How do you reverse a float number?
Convert the float into an integer and devisor model, reverse the integer part using standard number reversing also and then apply devisor..
Eg: 3.14159..
Get decimal location by chceking the number this number is lessthan 3 so exponent can be 10^1..
If a float is 956.123 then exponent would be 10^3..
Reverse the int -> 951413..
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..
What is __ reversed __ in Python?
Python reversed() method
The reversed() method returns the reversed iterator of the given sequence. It is the same as the iter() method but in reverse order. Internally, it calls the __reversed__() method of the sequence class.
How do you reverse a 4 digit number 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.