I have an algorithm here that'll let me compute the reverse of a given set of integers without using strings or arrays (which is the goal). However, I've no idea how to work recursion into this function. It's driving me up a wall.
-----------------------------------------
#include <iostream.h>
#include <stdlib.h>
#include <limits.h>
void ReverseDigits(int number);
int main()
{
char reply;
int number;
do
{
cout << "Enter a number between 0 and "
<< INT_MAX << ": ";
cin >> number;
cin.ignore(999, '\n');
while (number < 0)
{
cerr << "Input error: value entered is not between 0 and "
<< INT_MAX << endl;
cout << "Enter a number between 0 and "
<< INT_MAX << ": ";
cin >> number;
cin.ignore(999, '\n');
}
cout << "The number written in the usual way: " << number << endl;
cout << "The number with its digits reversed: ";
ReverseDigits(number);
cout << endl;
cout << "Do another customer? (n = no, any other = yes) ";
cin >> reply;
cin.ignore(999, '\n');
}
while (reply != 'n' && reply != 'N');
cout << "Press Enter or Return when ready..." << endl;
cin.get();
return EXIT_SUCCESS;
}
// write definition for ReverseDigits
// Here's where I get lost. How do I make this recursive?
void ReverseDigits(int number)
{
int result = 0;
while ( value % 10 )
{
result *= 10;
result += value % 10;
value /= 10;
}
}