reversing a number

hi! i'am a beginner as you can see, otherwise i probably wouldn't have such a stupid problem :) i have been seeking for a method how i could reverse a number for example 2345 to 5432 and check whether it is the same after the modification, read the same way from both ends. i have been trying to implement string and arrays but can't seem to figure out how to do it. help will be much appreciated. :)
What do you have so far if anything?
Off the top of my head I'd just convert the number to a string. Reverse the string and convert back a number.
well, that was my idea too :) but i can't figure out how to do it. i would need to write every digit in a different slot of that char array wouldn't i? 2 - array[0], 3 - array[1] and so far i haven't been able to do it. i you could be so kind and write an example of how it's done.
closed account (z05DSL3A)
How about something like (pseudocode)

reverse = 0
WHILE number IS NOT Zero
    reverse * 10
    reverse += number Mod 10
    number = number Div 10
WEND
IF reverse == number ...
Ok tried to write the program with module and div BUT when i hit the RUN button console opens but nothing is displayed... can't figure it out

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int main()
{
    int nProduct; // the product of those two number
    int nRev = 0; // reversed version of the nProduct

    for ( int nNum1 = 999; nNum1 > 800; nNum1--)
    {
        for ( int nNum2 = 999; nNum2 > 800; nNum2--)
        {
            nProduct = nNum1 * nNum2;
            int nProductT = nProduct; // temporary product
            div_t nRevDT;
            int nRevT; // temporary reversed value
            nRevT = 0;
            while (nProductT != 0)
            {
                nRevT *= 10;
                nRevDT = div (nProductT, 10);
                nRevT += nRevDT.rem;
                nProductT = nProductT % 10;
            }
            if ( nProduct == nRevT)
            {
                if (nRevT > nRev)
                {
                    nRev = nRevT;
                }
            }
            cout << "sub result : the reversed version of the number " << nProduct << " is " << nRevT << endl;
        }
    }

    cout << "the largest palindrome made from the product of two 3-digit numbers : "
    << nRev << endl << endl;

    system("pause");
    return 0;
}
Something like this using strings?
1
2
3
4
5
6
std::string n = "101";
std::string n2 = n;
std::reverse( n2.begin(), n2.end() );
if( n == n2 ) {
    // palindrome
}
Last edited on
nProductT = nProductT % 10; if a<b then a%b == a
ok! :) changed that part, but it still doesn't work as it should (i want it to) work.

1
2
3
4
5
6
7
8
 do
            {
                nRevT *= 10;
                nRevDT = div (nProductT, 10);
                nRevT += nRevDT.rem;
                nProductT = nProductT % 10;
            }
            while (nProductT > 10);


now it run through all the loops but the output is only one digit and the result is 0

Something like this using strings?
yeah but how do i transfer my int into the string?
http://cplusplus.com/articles/D9j2Nwbp/

The streamstream method is what i would recommend.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main (int argc, const char * argv[])
{
    int x = 123456;
    int y =0;
    
    while (x > 0) {
        y *= 10;
        y += x % 10;
        x /= 10;
    }
    std::cout << y << std::endl;
    
    return 0;
}
Ah project euler....a FABULOUS way to learn C++

Try this...

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
31
#include <iostream>

using namespace std;

long reverse(long num, int base=10) {
  long val=0;
  while (num != 0) {
    val *= base;
    val += num % base;
    num /= base;
  }
  return val;
}

int main(int argc, char **argv) {

  int biggest=0;
  for (int i=100; i<1000; i++) {
    for (int j=i; j<1000; j++) {
      long prod=i*j;
      if (prod == reverse(prod)) {
        cout << "found palindrome : " << prod << endl;
        if (prod > biggest) {
          biggest=prod;
        }   
      }   
    }   
  }   
  cout << "biggest is " << biggest << endl;
  return 0;
}


Note i : A 3 digit number is anything from 100 to 999
Note ii : If you've done 100*300 there's no point doing 300*100 hence starting the inner for loop @ i
Note iii : Depending on your platform the product of two 3 digit numbers may overflow with int hence use long.
Note iv : The reverse will work in any base. As an exercise, modify the program to find palindromes in hexadecimal and print them in hex too....
Note v : Doing this by reversing the string representation of the number is a cheat, and is also way slower and uses more memory.

Happy Days!
ok, it seems that my code was more or less the same as what emyr666 wrote and finally i got my answer :D although i still need to learn how to do it with strings... just one thing, it worked when i changed this part with while () but it didn't work before with do while ()

That is - this way it worked
1
2
3
4
5
6
 while (nProductT != 0)
            {
                nReverse *= 10;
                nReverse += nProductT % 10;
                nProductT = nProductT / 10;
            }


while this way - didn't
1
2
3
4
5
6
7
 do
            {
                nReverse *= 10;
                nReverse += nProductT % 10;
                nProductT = nProductT / 10;
            }
while (nProductT > 10);
Topic archived. No new replies allowed.