Your heading indicates that you have not solved the task accordingly. According to this, several numbers are to be read and then reverse output one after the other.
Of course you could write the function reverse() with a recursive call, even if this is perhaps unnecessary. Unfortunately, however, some things go wrong with you also in the function.
int reverse(int n)
{
int rev_num = 0;
while (n != -1)
return n;
if (n == -1) {
rev_num = rev_num * 10 + n % 10;
n = n / 10;
return reverse(rev_num);
}
}
The while loop makes little sense. If it should be true, the return is used.
If n then actually has the value -1, the function would be called again recursively in the if() query? This makes little sense and does not work like this.
A working recursive function could perhaps look like this:
int reverse(int n)
{
if (abs(n) < 10)
return n; else {
int i=1;
while ( n / (i*10)) i *= 10;
return ((n % 10) * i + reverse(n / 10));
}
}
You should look at the whole thing step by step with the debugger and think about what is actually happening.
With the solution approach with the division by 10 and the modulo operator, I assumed a change of the sequence of the digits. But you could also understand "output all numbers in reverse order" in a way, that you don't output the digits in the number, but the complete numbers in reverse order. The second version would be much easier to realize and would work completely different.