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.

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>

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
1
2
3
4
5
6
7
8
9
10
11
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.
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
34
35
36
37
38
39
40
41
42
#include <iostream>

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.