Open in App
Not now

# Reverse digits of an integer with overflow handled | Set 2

• Last Updated : 28 Jan, 2022

Given a 32-bit integer N. The task is to reverse N, if the reversed integer overflows, print -1 as the output.

Examples

Input: N = 123
Output: 321

Input: N = -123
Output: -321

Input: N = 120
Output: 21

Approach: Unlike approaches Set 1 of the article, this problem can be solved simply by using a 64-bit data structure and range of int data type [-2^31, 2^31 – 1]

• Copy the value of the given number N in a long variable
• Check if it is negative
• Now reverse the long number digit by digit, and if at any step, its value goes out of range the int type, return 0.
• Make the resultant number negative if the given number is negative
• Again check for number to be in int range. If yes return 0, else return the reversed number.

Below is the implementation of the above approach.

## C++

 `// C++ program for above approach` `#include `   `// Function to reverse digits in a number` `int` `reverseDigits(``int` `N)` `{` `    ``// Taking a long variable` `    ``// to store the number` `    ``long` `m = N;`   `    ``int` `neg = m < 0 ? -1 : 1;`   `    ``if` `(m * neg < ``pow``(-2, 31)` `        ``|| m * neg >= ``pow``(2, 31))` `        ``return` `0;` `    ``m = m * neg;` `    ``long` `n = 0;`   `    ``while` `(m > 0) {` `        ``if` `(n * 10 < ``pow``(-2, 31)` `            ``|| n * 10 >= ``pow``(2, 31))` `            ``return` `0;` `        ``n = n * 10 + m % 10;` `        ``m = m / 10;` `    ``}` `    ``if` `(n * neg < ``pow``(-2, 31)` `        ``|| n * neg >= ``pow``(2, 31))` `        ``return` `0;` `    ``n = n * neg;` `    ``return` `n;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 5896;` `    ``printf``(``"Reverse of no. is %d"``,` `           ``reverseDigits(N));` `    ``return` `0;` `}`

## Java

 `// Java program for above approach` `class` `GFG {`   `  ``// Function to reverse digits in a number` `  ``static` `int` `reverseDigits(``int` `N) {`   `    ``// Taking a long variable` `    ``// to store the number` `    ``long` `m = N;`   `    ``int` `neg = m < ``0` `? -``1` `: ``1``;`   `    ``if` `(m * neg < Math.pow(-``2``, ``31``)` `        ``|| m * neg >= Math.pow(``2``, ``31``))` `      ``return` `0``;` `    ``m = m * neg;` `    ``long` `n = ``0``;`   `    ``while` `(m > ``0``) {` `      ``if` `(n * ``10` `< Math.pow(-``2``, ``31``)` `          ``|| n * ``10` `>= Math.pow(``2``, ``31``))` `        ``return` `0``;` `      ``n = n * ``10` `+ m % ``10``;` `      ``m = m / ``10``;` `    ``}` `    ``if` `(n * neg < Math.pow(-``2``, ``31``)` `        ``|| n * neg >= Math.pow(``2``, ``31``))` `      ``return` `0``;` `    ``n = n * neg;` `    ``return` `(``int``) n;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String args[]) {` `    ``int` `N = ``5896``;` `    ``System.out.println(``"Reverse of no. is "` `+ reverseDigits(N));` `  ``}` `}`   `// This code is contributed by Saurabh Jaiswal`

## Python3

 `# Python code for the above approach`   `# Function to reverse digits in a number` `def` `reverseDigits(N):`   `    ``# Taking a long variable` `    ``# to store the number` `    ``m ``=` `N`   `    ``neg ``=` `-``1` `if` `m < ``0` `else` `1`   `    ``if` `(m ``*` `neg < (``-``2` `*``*` `31``) ``or` `m ``*` `neg >``=` `(``2` `*``*` `31``)):` `        ``return` `0` `    ``m ``=` `m ``*` `neg` `    ``n ``=` `0`   `    ``while` `(m > ``0``):` `        ``if` `(n ``*` `10` `< (``-``2` `*``*` `31``) ``or` `n ``*` `10` `>``=` `(``2` `*``*` `31``)):` `            ``return` `0` `        ``n ``=` `n ``*` `10` `+` `m ``%` `10` `        ``m ``=` `(m ``/``/` `10``)` `    ``if` `(n ``*` `neg < (``-``2` `*``*` `31``) ``or` `n ``*` `neg >``=` `(``2` `*``*` `31``)):` `        ``return` `0` `    ``n ``=` `n ``*` `neg` `    ``return` `n`   `# Driver Code` `N ``=` `5896` `print``(f``"Reverse of no. is  {reverseDigits(N)}"``)`   `# This code is contributed by gfgking`

## C#

 `// C# program for above approach` `using` `System;` `class` `GFG` `{`   `  ``// Function to reverse digits in a number` `  ``static` `int` `reverseDigits(``int` `N)` `  ``{`   `    ``// Taking a long variable` `    ``// to store the number` `    ``long` `m = N;`   `    ``int` `neg = m < 0 ? -1 : 1;`   `    ``if` `(m * neg < Math.Pow(-2, 31)` `        ``|| m * neg >= Math.Pow(2, 31))` `      ``return` `0;` `    ``m = m * neg;` `    ``long` `n = 0;`   `    ``while` `(m > 0) {` `      ``if` `(n * 10 < Math.Pow(-2, 31)` `          ``|| n * 10 >= Math.Pow(2, 31))` `        ``return` `0;` `      ``n = n * 10 + m % 10;` `      ``m = m / 10;` `    ``}` `    ``if` `(n * neg < Math.Pow(-2, 31)` `        ``|| n * neg >= Math.Pow(2, 31))` `      ``return` `0;` `    ``n = n * neg;` `    ``return` `(``int``)n;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main()` `  ``{` `    ``int` `N = 5896;` `    ``Console.Write(``"Reverse of no. is "` `+ reverseDigits(N));` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`Reverse of no. is 6985`

Time Complexity: O(D), where D is the number of digits in N.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles