Flip Last Digit of an Integer to front

Pages: 12
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;
}
Could make this super easy with a stringstream.
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
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.
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 );
}
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
@ResidentBiscuit

Do you think that this code is valid?!

1
2
3
std::string flipped = ss.str();
...
return (int)flipped;
Good catch, the important part is valid though. I didn't know if I could cast from string to int like that.
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
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;
}

@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



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?
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.

Guys,
but when i run this unsigned long long in my windows - 7, 64Bit machine it won't compile and gives me errors, LOL
Errors such as?
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
And what's the code that caused this?
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
Change the loop the following way

for ( unsigned long long i = 1; i < 2147483647ull; i++ ) {
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