is there a way to read the number from his other side?!!

i wanna know if i could read any int from his both sides like "234" i want to record the inversed number like"432" is there a way to read the number from his other side?
Hi,
you can use to_string() function, reverse the order of chars, and finally convert the contents into new int by using atoi() function.

edit:

following if fully working example:

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
#include <string>
#include <iostream>
#include <cstdlib>

int main()
{
	using namespace std;

	int number = 123456789; // initial value to be reversed
	string input = to_string(number); // convert number to string
	string output = "";
	char temp;
	int reversed_int; // will be: 987654321

	for (size_t i = input.size(); i > 0; --i)
	{
		temp = input.at(i -1); // get current character
		output += temp; // appending in reverse order happens here
	}

	reversed_int = atoi(output.c_str()); // convert reversed number back to int

	// show result
	cout << "original number was: " << number << endl;
	cout << "in reversed order it is: " << reversed_int << endl;

	cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.