How do I reverse digits of an integer?

Reverse digits of an integer.

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

1
2
3
4
5
6
7
      int reverse(int x) {
        int output;
        for(int i; i< x.size(); i++ ){
          output = x[i]+output;  
        }
        cout<<output;
}


I know this is wrong, but i thought reversing an INT will be pretty similar like reversing an STRING. HELP?
how do I reverse digits of an integer?
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c

Convert the original number to a string then reverse the characters. Keep in mind that depending on the nature of your problem you might have to convert the reversed string back to an int.

Another way is to deompose the number to an array (vector, list whatever) of int's using modulo 10 (assuming base 10) arithmetic.
I want to reverse digits of an integer. How do I do it?
closed account (48T7M4Gy)
You've just been shown. This is not a homework site!
closed account (LA48b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
    int x = 123;

    stringstream ss;
    ss << x;

    string str;
    ss >> str;

    reverse(str.begin(), str.end());

    ss.clear();

    ss << str;
    ss >> x;

    cout << x << '\n';
}
Not a clever solution, @krako.
reverse(str.begin(), str.end());
What if x == -123? Then the string would be reversed to 321-. Then you input that invalid number. And the final result is wrong; it is not what the OP wants.
closed account (LA48b7Xj)
My deepest apologies SakurasouBusters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
    int x = -123;
    bool neg = x < 0 ? true : false;

    stringstream ss;
    ss << x;

    string str;
    ss >> str;

    reverse(str.begin(), str.end());

    ss.clear();

    ss << str;
    ss >> x;

    if(neg)
        x = -x;

    cout << x << '\n';
}
No need to convert to strings or arrays or use stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int x = 38492;
    bool negative = false;
    if(x < 0) 
    {
        negative = true;
        x = -x;
    }
    
    int reversed = 0;
    while(x > 0)
    {
        reversed = reversed*10 + x%10;
        x /= 10;
    }
    
    if(negative) reversed = -reversed;
    cout << reversed << '\n';
}
closed account (LA48b7Xj)
Very nice Arslan :)
Thank you Arslan. This is really helpful. However I have a question, what does this line of code do : reversed = reversed*10 + x%10;
It is kinda confusing for me.
Krako I know it is not a homework site. And this is NOT HOMEWORK! I'm just learning C++! If you do not want to reply. Just simply ignore my post.

Thank you guys
Thank you Arslan. This is really helpful. However I have a question, what does this line of code do : reversed = reversed*10 + x%10;


The % operator (called modulo) returns remainder after division. Thus:

7 % 3 = 1 (3 goes into 7 twice, leaving a remainder of 1).
15 % 5 = 0 (5 goes into 15 thee times, leaving a remainder of 0).

This operator can come in really handy for manipulating integers. Anyway, you'll notice that modding any number by 10 will give you its last digit.

1
2
3
4
5
6
    int reversed = 0;
    while(x > 0)
    {
        reversed = reversed*10 + x%10; // take last digit of x, "appends" it to reversed
        x /= 10; // remove last digit of x
    }


Since reversed starts off as 0, the result of the first iteration is that the last digit of x is simply added to 0. The last digit of x is then "removed" by dividing by 10.

After that, in the second iteration, reversed is multiplied by 10 and the last digit of x is added to it. The last digit of x is then removed.

This is continued until x is 0.

The loop essentially takes advantage of how base 10 numbers work. To form any number, you can start at 0, and repeatedly by 10 and add each successive digit.

To get from 0 to 236 for example, you can do the following:
0*10 + 2 = 2;
2*10 +3 = 23;
23*10 + 6 = 236.

This is what the loop above is doing, except its always adding the last digit of x, thus resulting in the reverse of x.
Last edited on
closed account (LA48b7Xj)
lol, I didn't mention homework that was kemort
closed account (48T7M4Gy)
Yep, it was me krako, you were unfairly singled out and accused.
I apologize. Thank you for all your replies! It is really helpful!
closed account (48T7M4Gy)
:) all good let's move on.
These are the exact steps to find reverse of a number:
1: Use a loop to access all digits of a number.
2: Inside loop, use modulus operator to extract digits one by one.
3: Add extracted digits to a new variable "reverse".
4: Divide the number by 10 so for next loop iteration, it will point to next digit.
5: multiply the "reverse" variable with 10 so digits will be placed not being summed up.

Here is a code example:

while(number!=0)
{
reverse=reverse*10;
reverse=reverse+number%10;
number=number/10;
}

I highly recommend you to read this post: http://www.cppbeginner.com/numbers/how-to-reverse-a-number-in-c

This link contains complete code, explanation, screenshots.and dry running.
Last edited on
Topic archived. No new replies allowed.