Write a program to reverse an integer assuming that the input is a 32-bit integer. If the reversed integer overflows, print -1 as the output.
Let us see a simple approach to reverse digits of an integer.
C++
#include <bits/stdc++.h>
using namespace std;
int reversDigits( 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 = 5896;
cout << "Reverse of no. is " << reversDigits(num);
return 0;
}
|
C
#include <stdio.h>
int reversDigits( 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 = 5896;
printf ( "Reverse of no. is %d" , reversDigits(num));
return 0;
}
|
Java
class GFG
{
static int reversDigits( 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 = 5896 ;
System.out.println( "Reverse of no. is "
+ reversDigits(num));
}
}
|
Python3
n = 5896 ;
rev = 0
while (n ! = 0 ):
a = n % 10
rev = rev * 10 + a
n = n / / 10
print (rev)
|
C#
using System;
class GFG
{
static int reversDigits( 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 = 5896;
Console.Write( "Reverse of no. is "
+ reversDigits(num));
}
}
|
PHP
<?php
function reversDigits( $num )
{
$rev_num = 0;
while ( $num != 0)
{
$rev_num = $rev_num * 10 +
$num % 10;
$num = (int) $num / 10;
}
return $rev_num /10;
}
$num = 5896;
echo "Reverse of no. is " ,
reversDigits( $num );
?>
|
Javascript
<script>
function reversDigits(num)
{
let rev_num = 0;
while (num != 0)
{
rev_num = rev_num*10 + num%10;
num = Math.floor(num/10);
}
return rev_num;
}
let num = 5896;
document.write( "Reverse of no. is " + reversDigits(num));
</script>
|
Output
Reverse of no. is 6985
Time Complexity: O(log(num))
Auxiliary Space: O(1)
However, if the number is large such that the reverse overflows, the output is some garbage value. If we run the code above with input as any large number say 1000000045, then the output is some garbage value like 1105032705 or any other garbage value. See this for the output.
How to handle overflow?
The idea is to store the previous value of the sum can be stored in a variable that can be checked every time to see if the reverse overflowed or not.
Below is the implementation to deal with such a situation.
C++
#include <bits/stdc++.h>
using namespace std;
int reversDigits( int num)
{
bool negativeFlag = false ;
if (num < 0)
{
negativeFlag = true ;
num = -num ;
}
int prev_rev_num = 0, rev_num = 0;
while (num != 0)
{
int curr_digit = num % 10;
rev_num = (rev_num * 10) + curr_digit;
if ((rev_num - curr_digit) /
10 != prev_rev_num)
{
cout << "WARNING OVERFLOWED!!!"
<< endl;
return 0;
}
prev_rev_num = rev_num;
num = num / 10;
}
return (negativeFlag == true ) ?
-rev_num : rev_num;
}
int main()
{
int num = 12345;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
num = 1000000045;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
return 0;
}
|
C
#include <stdio.h>
int reversDigits( int num)
{
bool negativeFlag = false ;
if (num < 0)
{
negativeFlag = true ;
num = -num ;
}
int prev_rev_num = 0, rev_num = 0;
while (num != 0)
{
int curr_digit = num%10;
rev_num = (rev_num*10) + curr_digit;
if ((rev_num - curr_digit)/10 != prev_rev_num)
{
printf ( "WARNING OVERFLOWED!!!\n" );
return 0;
}
prev_rev_num = rev_num;
num = num/10;
}
return (negativeFlag == true )? -rev_num : rev_num;
}
int main()
{
int num = 12345;
printf ( "Reverse of no. is %d\n" , reversDigits(num));
num = 1000000045;
printf ( "Reverse of no. is %d\n" , reversDigits(num));
return 0;
}
|
Java
class ReverseDigits
{
static int reversDigits( int num)
{
boolean negativeFlag = false ;
if (num < 0 )
{
negativeFlag = true ;
num = -num ;
}
int prev_rev_num = 0 , rev_num = 0 ;
while (num != 0 )
{
int curr_digit = num% 10 ;
rev_num = (rev_num* 10 ) + curr_digit;
if ((rev_num - curr_digit)/ 10 != prev_rev_num)
{
System.out.println( "WARNING OVERFLOWED!!!" );
return 0 ;
}
prev_rev_num = rev_num;
num = num/ 10 ;
}
return (negativeFlag == true )? -rev_num : rev_num;
}
public static void main (String[] args)
{
int num = 12345 ;
System.out.println( "Reverse of no. is " + reversDigits(num));
num = 1000000045 ;
System.out.println( "Reverse of no. is " + reversDigits(num));
}
}
|
Python3
def reversDigits(num):
negativeFlag = False
if (num < 0 ):
negativeFlag = True
num = - num
prev_rev_num = 0
rev_num = 0
while (num ! = 0 ):
curr_digit = num % 10
rev_num = (rev_num * 10 ) + curr_digit
if (rev_num > = 2147483647 or
rev_num < = - 2147483648 ):
rev_num = 0
if ((rev_num - curr_digit) / / 10 ! = prev_rev_num):
print ( "WARNING OVERFLOWED!!!" )
return 0
prev_rev_num = rev_num
num = num / / 10
return - rev_num if (negativeFlag = = True ) else rev_num
if __name__ = = "__main__" :
num = 12345
print ( "Reverse of no. is " ,reversDigits(num))
num = 1000000045
print ( "Reverse of no. is " ,reversDigits(num))
|
C#
using System;
class GFG
{
static int reversDigits( int num)
{
bool negativeFlag = false ;
if (num < 0)
{
negativeFlag = true ;
num = -num ;
}
int prev_rev_num = 0, rev_num = 0;
while (num != 0)
{
int curr_digit = num % 10;
rev_num = (rev_num * 10) +
curr_digit;
if ((rev_num - curr_digit) / 10 != prev_rev_num)
{
Console.WriteLine( "WARNING OVERFLOWED!!!" );
return 0;
}
prev_rev_num = rev_num;
num = num / 10;
}
return (negativeFlag == true ) ?
-rev_num : rev_num;
}
static public void Main ()
{
int num = 12345;
Console.WriteLine( "Reverse of no. is " +
reversDigits(num));
num = 1000000045;
Console.WriteLine( "Reverse of no. is " +
reversDigits(num));
}
}
|
Javascript
<script>
function reversDigits(num)
{
let negativeFlag = false ;
if (num < 0)
{
negativeFlag = true ;
num = -num ;
}
let prev_rev_num = 0, rev_num = 0;
while (num != 0)
{
let curr_digit = num % 10;
rev_num = (rev_num * 10) + curr_digit;
if (rev_num >= 2147483647 ||
rev_num <= -2147483648)
rev_num = 0;
if (Math.floor((rev_num - curr_digit) / 10) != prev_rev_num)
{
document.write( "WARNING OVERFLOWED!!!"
+ "<br>" );
return 0;
}
prev_rev_num = rev_num;
num = Math.floor(num / 10);
}
return (negativeFlag == true ) ?
-rev_num : rev_num;
}
let num = 12345;
document.write( "Reverse of no. is "
+ reversDigits(num) + "<br>" );
num = 1000000045;
document.write( "Reverse of no. is "
+ reversDigits(num) + "<br>" );
</script>
|
Output
Reverse of no. is 54321
Reverse of no. is 1105032705
Time Complexity: O(log(num))
Auxiliary Space: O(1)
Efficient Approach :
The above approach won’t work if we are given a signed 32-bit integer x, and return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 – 1], then return 0. So we cannot multiply the number*10 and then check if the number overflows or not.
We must check the overflow condition before multiplying by 10 by using the following logic :
You are checking the boundary case before you do the operation. (reversed >INT_MAX ) wouldn’t work because reversed will overflow and become negative if it goes past MAX_VALUE. Dividing MAX_VALUE by 10 lets you check the condition without overflowing INT_MAX is equal 2147483647. INT_MIN is equal -2147483648. The last digits are 7 and 8. This is the reason why we also check rem > 7 and rem < -8 conditions
C++
#include <bits/stdc++.h>
using namespace std;
int reversDigits( int num) {
int rev = 0 ;
while (num != 0){
int rem = num % 10 ;
num /= 10 ;
if (rev > INT_MAX/10 || rev == INT_MAX/10 && rem > 7){
return 0 ;
}
if (rev < INT_MIN/10 || rev == INT_MIN/10 && rem < -8){
return 0 ;
}
rev = rev*10 + rem ;
}
return rev ;
}
int main()
{
int num = 12345;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
num = 1000000045;
cout << "Reverse of no. is "
<< reversDigits(num) << endl;
return 0;
}
|
Java
public class GFG
{
static int reversDigits( int num)
{
int rev = 0 ;
while (num != 0 ){
int rem = num % 10 ;
num /= 10 ;
if (rev > Integer.MAX_VALUE/ 10 || rev == Integer.MAX_VALUE/ 10 && rem > 7 ){
return 0 ;
}
if (rev < Integer.MIN_VALUE/ 10 || rev == Integer.MIN_VALUE/ 10 && rem < - 8 ){
return 0 ;
}
rev = rev* 10 + rem ;
}
return rev ;
}
public static void main (String[] args)
{
int num = 12345 ;
System.out.println( "Reverse of no. is " + reversDigits(num) );
num = 1000000045 ;
System.out.println( "Reverse of no. is " + reversDigits(num) );
}
}
|
C#
using System;
public class GFG
{
static int reversDigits( int num)
{
int rev = 0 ;
while (num != 0){
int rem = num % 10 ;
num /= 10 ;
if (rev > Int32.MaxValue/10 || rev == Int32.MaxValue/10 && rem > 7){
return 0 ;
}
if (rev < Int32.MinValue/10 || rev == Int32.MinValue/10 && rem < -8){
return 0 ;
}
rev = rev*10 + rem ;
}
return rev ;
}
public static void Main ( string [] args)
{
int num = 12345;
Console.WriteLine( "Reverse of no. is " + reversDigits(num) );
num = 1000000045;
Console.WriteLine( "Reverse of no. is " + reversDigits(num) );
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
const INT_MIN = -2147483648;
function reversDigits(num) {
let rev = 0;
while (num > 0){
let rem = num % 10 ;
num = Math.floor(num/10) ;
if (rev > INT_MAX/10 || rev == INT_MAX/10 && rem > 7){
return 0 ;
}
if (rev < INT_MIN/10 || rev == INT_MIN/10 && rem < -8){
return 0 ;
}
rev = rev*10 + rem ;
}
return rev ;
}
let num = 12345;
document.write(`Reverse of no. is ${reversDigits(num)}`, "</br>" );
num = 1000000045;
document.write(`Reverse of no. is ${reversDigits(num)}`, "</br>" );
</script>
|
Output
Reverse of no. is 54321
Reverse of no. is 0
Time Complexity: O(log(num))
Auxiliary Space: O(1)
This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...