Flip Last Digit of an Integer to front

Pages: 12
Jul 18, 2012 at 6:02pm
Hi Guys,
I need to flip an integer's last digit to front and print out new integer:
ie.
Lets says my original integer is 19896 then i like to have a out put something like 61989. Can someone please help me. If not a code then algorithm that follows the same method.
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void flip(double &no)
{
int c=0,s=no%10;
no/=10;
while(pow(10,c)<no) c++;
no+=s*pow(10,c);
}
int main()
{
int no=45867;
flip(no);
cout << no;
return 0;
}
Jul 18, 2012 at 6:21pm
Could make this super easy with a stringstream.
Jul 18, 2012 at 6:22pm
To switch more numbers in the back to the front, just divide the number by ten again

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	int num, digit;
	cout << "Enter a integer: ";
	cin >> num;
	cout << endl;

	digit = num%10;
	num /= 10;
	cout << digit << num;
}


It's not really one number, just two numbers put together
Last edited on Jul 18, 2012 at 6:23pm
Jul 18, 2012 at 7:56pm
Thank you for helping but i need to have it in a number not two number because i need to compare this new number with something.
Jul 18, 2012 at 8:38pm
Try this code

1
2
3
4
5
6
7
8
9
int flip( int n )
{
	int digit = n % 10;
	int r = n; 
	while ( r /=10 ) digit *= 10;
	n = digit + n / 10;

	return ( n );
}
Jul 18, 2012 at 9:01pm
1
2
3
4
5
6
7
8
9
10
11
int flip(int n)
{
   std::stringstream ss;
   ss << n;
   std::string flipped = ss.str();
   int lastIndex = flipped.length - 1;
   char tmp = flipped[0];
   flipped[0] = flipped[lastIndex];
   flipped[lastIndex] = tmp;
   return (int)flipped;
}


stringstream way. Wrote it right here in the text box, so don't hate if the formatting is weird.
Last edited on Jul 18, 2012 at 9:03pm
Jul 18, 2012 at 9:39pm
@ResidentBiscuit

Do you think that this code is valid?!

1
2
3
std::string flipped = ss.str();
...
return (int)flipped;
Jul 18, 2012 at 9:57pm
Good catch, the important part is valid though. I didn't know if I could cast from string to int like that.
Jul 19, 2012 at 2:07am
I have learned that it is wise to test my own code before posting it.

@vlad
You also might want to reconsider line 6 of your code. This is where the stringstream might be handy.
Last edited on Jul 19, 2012 at 2:07am
Jul 19, 2012 at 2:03pm
thanks Guys for helping me.
Here is one more thing. How could one fit this integer?
421052631578947368.

What should i use, int, float, double??????

and this is what i'm trying to do:
Lets find a number that is same when you double it and flip the last digit of original number to the front.
ie.
my Original number is 19896, but when i flip last digit it will become 61989 and double the original is (19896*2 = 39792) but the flipped number which is 61989 and doubled number which is 39792 are not EQUAL so it does not work. find me a number that is equal.

Thanks

My code to do the following is posted.
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
#include <iostream>
#include <cmath>

using namespace std;

int flip( int n )
{
	int digit = n % 10;
	int r = n; 
	while ( r /=10 ) digit *= 10;
	n = digit + n / 10;

	return ( n );
}

int main(){
  int reverse_number=0, twice_number =0;  
  for(int i = 1; i< 2147483647;i++){
  twice_number = i*2;
  reverse_number = flip(i);
  
  if(reverse_number == twice_number)
  {
	cout<<"Match found :"<<"Original ->"<<i<<" Reverse ->"<<reverse_number<<" Double ->"<<twice_number<<endl;
	break;
  }
  else{
		cout<<"Scanning..."<<endl;
	  }
}
 
return 0;
}
Jul 19, 2012 at 2:04pm

@Duoas
@vlad
You also might want to reconsider line 6 of your code. This is where the stringstream might be handy.


There is no need to introduce new headers, template classes and objects when an operation can be simply done with existent operators for a given type. What you are suggesting is called a bad style. There is such a principle as KISS in programming that means Keep It Simple Stuipid



Jul 19, 2012 at 2:08pm
Here is one more thing. How could one fit this integer?
421052631578947368.

Unsigned long long should fit this.

Lets find a number that is same when you double it and flip the last digit of original number to the front.

This is really the same problem as the original one, with another step added to it. And from glancing at your program, looks like you're on the right track. Are you having issues with it?
Jul 19, 2012 at 2:15pm
The maximum value of unsigned long long is equal to 18446744073709551615. It is much bigger than
421052631578947368.

So you can use unsigned long long instead of int.

Jul 19, 2012 at 2:27pm
Guys,
but when i run this unsigned long long in my windows - 7, 64Bit machine it won't compile and gives me errors, LOL
Jul 19, 2012 at 2:28pm
Errors such as?
Jul 19, 2012 at 2:32pm
Sorry i should have posted my error first:

C:\Users\Jatin\Desktop>g++ -Wall test2.cpp
test2.cpp: In function `int main()':
test2.cpp:17: warning: integer constant out of range
test2.cpp:17: warning: decimal integer constant is so large that it is unsigned
Jul 19, 2012 at 2:38pm
And what's the code that caused this?
Jul 19, 2012 at 2:43pm
closed account (o3hC5Di1)
Taken from his earlier code I would think this bit:

1
2
3
4
  int reverse_number=0, twice_number =0;  
  for(int i = 1; i< 2147483647;i++){
  twice_number = i*2;
  reverse_number = flip(i);


That's way too large for a regular int, you should probably use type unsigned long for this ( ? ).
(someone please correct me if I'm wrong)

All the best,
NwN
Jul 19, 2012 at 2:48pm
Change the loop the following way

for ( unsigned long long i = 1; i < 2147483647ull; i++ ) {
Jul 19, 2012 at 2:51pm
closed account (o3hC5Di1)
Hi Vlad,

Just curious here - wouldn't 'twice_number' and "reverse_number" also have to be declared as long's?

All the best,
NwN
Pages: 12