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:
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?
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!
#include <iostream>
usingnamespace 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';
}
#include <iostream>
usingnamespace 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';
}