checking inverse

Mar 20, 2017 at 10:36pm
hello, Im trying to code a loop that would take in a number then divide that number to 10. After that, I create an inverse of that number to see if it match and every time the number being divided by 10, i++. It seems that the inverse never forms fully before it gets checked. Any help on this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;
int temp1, y, temp2, rev1,i = 0;
int x;
int main()
{
    cin >> x;
  do
{
	temp1= x/10;
	y = temp1;
	temp2 = y % 10;
	rev1 = (rev1 * 10) + temp2;
	i++;
}
	while (temp1!= rev1 );
cout <<  i;
   
   return 0;
}
Last edited on Mar 20, 2017 at 10:37pm
Mar 21, 2017 at 12:39am
are you trying to do something like

1234
becomes
4321

and then check if 1234 == 4321 (it doesn't in this example) ?

num = x;
rev = 0;
while (num != 0) //this is pretty much what you have, I think, a little tighter.
{
rev *= 10;
rev += num%10;
num /= 10;
}
if (rev == x) //move this outside the loop?
...





Mar 21, 2017 at 1:26am
Im trying something similar.
for example, 1234 becomes 123 then inverses of that which is 321 then check to see if they are the same if not continuing to cut down the right hand most digit then check again until they are the same. Also, seeing how many time it needed to cut down the last digit for it to be the same.
Topic archived. No new replies allowed.