How to reverse a number? | SoloLearn: Learn to code for FREE!

0

How to reverse a number?

example : 571 ---> 175

c++

8/16/2016 6:47:56 AM

Zafirah Mohamed iqbal

2 Answers

New Answer

+5

You could take in the number as a string, and reverse it from there. Note that this will reverse literally any string, not just numbers: #include <iostream> #include <string> using namespace std; int main() { string num; int numLgth; cout << "Enter a number: "; getline(cin, num); numLgth = num.length(); for (int i = numLgth - 1; i >= 0; i--) cout << num.at(i); }

+4

other way: #include <iostream> #include <string> #include <algorithm> using namespace std; int main(void) { int num; cin >> num; string tmp = to_string(num); reverse(tmp.begin(), tmp.end()); cout << tmp << endl; return 0; }