### Reverse digits

I need to take the integer and output the digits in reverse. For example, if the input is 245 then it should output 542. I've determined the number of digits, but I'm not sure what to do next.

It's also telling me that there's an undefined reference to reverseDigit, and won't compile.

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; int reverseDigit (int); int main(int argc, char** argv) { int num = 0; cout << "Please enter an integer. "; cin >> num; reverseDigit(num); cout << "There are " << num << " digits." << endl; return 0; } int reversDigit (int num) { int size = 0; for (size = 0; num > 0; size++) { num /= 10; } return size; } ``````
Last edited on
 ``1234567891011`` ``````int ReverseDigits(int x) { int out = 0; while (x > 0) { out *= 10; out += x%10; x /= 10; } return out; }``````
Last edited on
in line 21 you've got "reversDigit", not the same as "reverseDigit" from line 5 - you're missing an "e"
Also, the solution above will lose trailing and leading 0s, so you'll need to include a check for that.
Thank you, but if I enter the code above it outputs the same number that was entered.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include using namespace std; int ReverseDigits(int x); int GetSize(int x); int main(int argc, char** argv) { int num = 0; cout << "Please enter an integer. "; cin >> num; cout << "There are " << GetSize(num) << " digits." << endl; cout << "The reverse is " << ReverseDigits(num) << endl; return 0; } int ReverseDigits(int x) { int out = 0; while (x > 0) { out *= 10; out += x%10; x /= 10; } return out; } int GetSize(int x) { int size; for (size = 0; x > 0; size ++) { x /= 10; } return size; }``````
Topic archived. No new replies allowed.