Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C
#include <stdio.h>
//
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);
        }
    
}

int main()
{
    int n,result;
    
    puts("Enter numbers: ");
    scanf("%d",&n);
    result = reverse(n);

    printf("result is : %d",result);
    return 0; 
}


What I have tried:

it just prints numbers not reverse of them :(
Posted
Updated 13-Oct-22 12:05pm
v2

You should implement a loop in the main function asking for numbers until the user enters -1.
For each valid number entered, your main function should call reverse.
Note your reverse function is flawed, you should implement a loop there as well:
pseudocode
reverse = 0
while number != 0
  reverse = 10 * reverse + number  % 10
  number = number / 10
end while
return reverse
 
Share this answer
 
To add to what CPallini has said, if your task is to print the reversed numbers after the user has entered a -1 and not before, then you need to store all the numbers as they are entered, and then print them afterwards.

This may help: C - Arrays[^]
 
Share this answer
 
Comments
CPallini 13-Oct-22 8:23am    
Indeed.
5.
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.
C
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:
C
int reverse(int n)
{
  if (abs(n) < 10)
     return n;	  // end recursion
  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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900