Recursive Function Reverse Integer

Apr 14, 2020 at 7:58pm
Assignment is to utilize recursive function to display a number in reverse. One function must output this reversed number, while the other must keep the first digit in place followed by the reversed remaining digits.

My program so far works with 4 digit numbers but not 3 or 5.
For example 1234 comes out how I want but not 123.

My logic is I can remove the first digit using %, reverse it, then throw the first digit back on again:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;

int x;
int first;
int num;
int newx;
int power;
int counter;
int temp;
int result;

void reverseDisplay1 (int x){

    if(x<10){
        cout << x;
    }
        else
        {
            cout << x % 10;
            reverseDisplay1(x / 10);
        }
}

void reverseDisplay2 (int newx){
    if(newx < 10){
        cout << newx;
    }
        else
        {
            cout << newx % 10;
            reverseDisplay2(newx / 10);
        }
}

int main()
{
    cout << "Please enter a number: ";
    cin >> x;
    first = x;
    while (first >= 10)
    {
        first = first / 10;
    }
    
    temp = x;
    while(temp != 0)
    {
        counter++;
        temp /= 10;
    }
    power = counter-1;
    result = pow(10, power);
    newx = x % result;
    
    cout << "reverseDisplay1 outputs:" << endl;
    reverseDisplay1(x);
    cout << '\n';
    
    cout << "reverseDisplay2 outputs:" << endl;
    cout << first;
    reverseDisplay2(newx);
    return 0;
}
Last edited on Apr 14, 2020 at 8:50pm
Apr 14, 2020 at 8:10pm
What the fuck is up with all those globals?
Jesus Christ!

Anyway, I'm unsure what you are trying to do.
You talk about reversing the number.
But you say you're keeping the first digit in place?
So 12345 would become 15432?
Is that right?
Last edited on Apr 14, 2020 at 8:10pm
Apr 14, 2020 at 8:11pm
result which is being used for % comes out as 99 when a 3 digit number is inputed. This is what is giving me the issue most likely.
Apr 14, 2020 at 8:22pm
exactly!
Apr 14, 2020 at 8:34pm
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
int main()
{
    cout << "Please enter a number: ";
    cin >> x;
    cout << "reverseDisplay1 outputs:" << endl;
    reverseDisplay1(x);
    cout << '\n';

    first = x;
    while (first >= 10)
    {
        first = first / 10;
    }

    temp = x;
    while(temp != 0)
    {
        counter++;
        temp /= 10;
    }
    power = counter-1;
    if (counter % 2 == 0)
    {
        result = pow(10, power);
    }
    if (counter % 2 != 0)
    {
        result = pow(10, power) + 1;
    }
    newx = x % result;
    cout << first;
    reverseDisplay2(newx);
    return 0;
}


Its working now, I know it's ugly but it gets the job done. Not trolling, just a beginner. If i could just get it to handle single digit inputs now Ill be golden. Thank you!
Apr 14, 2020 at 8:58pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
   int x;
   cout << "Please enter a number: ";   cin >> x;
   int result = 0, power = 1;
   while ( x >= 10 )
   {
      result = 10 * result + x % 10;
      x /= 10;
      power *= 10;
   }
   result += x * power;
   cout << "Result: " << result << '\n';
}
Apr 14, 2020 at 9:06pm
For the following inputs, what are the expected outputs?
12345
0
3
300
Apr 14, 2020 at 9:14pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int swivel( int x )
{
   int result = 0, power = 1;
   while ( x >= 10 )
   {
      result = 10 * result + x % 10;
      x /= 10;
      power *= 10;
   }
   return result + x * power;
}


int main()
{
   for ( int x : { 12345, 0, 3, 300 } ) cout << x << " : " << swivel( x ) << '\n';
}


12345 : 15432
0 : 0
3 : 3
300 : 300
Topic archived. No new replies allowed.