Algoritmo C para devolver un número

I have this algorithm

int oglinda(int n)
{
    if(n>9)
    {

        cout << n%10;
        oglinda(n/10);
    }
}

and it returns a number from right to left for example for 341=>143. Here's the problem: When the algorithm reaches the last number (which is < 9) it shouldn't receive it's value but instead it does. How comes that when the number is less than 9?

preguntado el 28 de mayo de 14 a las 12:05

Turn on all the warnings of your compiler. Your function says it returns a value, but there is no value returned!!! -

Also choose your language wisely: cout is not part of the C language; << is shift left ;) -

Your code is C++ but tagged with C? -

i made a mistake so what?don't be so harsh. -

2 Respuestas

Please try using the code below:

int oglinda(int n)
{
    if(n>9)
    {
        cout<<n%10;
        oglinda(n/10);
    } else{
        cout<<n;
    }
    return 0;
}

contestado el 28 de mayo de 14 a las 12:05

Try with this code: If you are using C compiler

 int oglinda(int n) {
     if(n>9) {
         printf("%d",n%10);    // Your tag is C and not C++
         return oglinda(n/10);
     } else {
         printf("%d",n);
     }
 return 0;    // You have to add return at the end of function
 }


Or Try with this code: If you are using C++ compiler

 int oglinda(int n) {
     if(n>9) {
         cout << n%10;    
         return oglinda(n/10);
     } else {
         cout << n;
     }
 return 0;    // You have to add return at the end of function
 }

contestado el 28 de mayo de 14 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.