Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse digits of an integer

Tags:

algorithm

how to reverse a number?

Example1: x = 123, return 321 Example2: x = -123, return -321

this is my answer:

public int reverse(int x) {
    int result = 0;
    while(x != 0){
        result = result * 10 + x % 10;
        x = x / 10;
    }
    return result;
}

but when I input 1534236469 , it will output 1056389759 , this is wrong. what do you think about my program? thanks.

like image 379
Jason Ni Avatar asked Feb 17 '15 23:02

Jason Ni


People also ask

How do you find the reverse digits?

Reverse an Integer In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by 10 times. Inside the loop, the reversed number is computed using: reverse = reverse * 10 + remainder; Let us see how the while loop works when n = 2345 .

How do you invert a number?

The inverse of a number A is 1/A since A * 1/A = 1 (e.g. the inverse of 5 is 1/5) All real numbers other than 0 have an inverse. Multiplying a number by the inverse of A is equivalent to dividing by A (e.g. 10/5 is the same as 10* 1/5)

How to reverse an integer in C++?

Example: C++ Program to Reverse an Integer. #include <iostream> using namespace std; int main() { int n, reversedNumber = 0, remainder; cout << "Enter an integer: "; cin >> n; while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; } cout << "Reversed Number = " << reversedNumber; return 0; }

How do you get the reversed number of a number?

In each iteration, the remainder when the value of n is divided by 10 is calculated, reversedNumber is computed and the value of n is decreased 10 fold. Let us see this process in greater detail: And so on, until n == 0. Finally, the reversedNumber (which contains the reversed number) is printed on the screen.

How do you reverse a negative number in a string?

So if the first character in the string is ‘-’, then the number is negative number, so reverse from index 1 to index length – 1. And finally convert them to integer before returning it, for positive number, simply reverse the string and make it integer before returning.

What if the reversed integer overflows?

If the reversed integer overflows, print -1 as the output. Let us see a simple approach to reverse digits of an integer . Attention reader! Don’t stop learning now.


1 Answers

One reason your program cannot give the right answer is that you store result in an int but you expect to be able to reverse the number 1534236469. The correct answer would be 9646324351, but that number is greater than the largest possible value of an int so you end up with something else. Try long long or try using input with no more than 9 digits.


Followup: I suggested long long because that will fairly reliably give you an 8-byte integer. You may also get 8 bytes in a long, depending on where you are building your code, but Visual C++ on 32-bit Windows (for example) will give you only 4 bytes. Possibly the 4-byte long will go the way of the 2-byte int soon enough, but at this point in time some of us still have to deal with it.

like image 163
David K Avatar answered Sep 21 '22 08:09

David K