Algoritmo C para devolver un número
Frecuentes
Visto 80 equipos
-4
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?
2 Respuestas
1
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
1
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 c++ or haz tu propia pregunta.
Turn on all the warnings of your compiler. Your function says it returns a value, but there is no value returned!!! - pmg
Also choose your language wisely:
cout
is not part of the C language;<<
is shift left ;) - pmgYour code is C++ but tagged with C? - user3451749
i made a mistake so what?don't be so harsh. - user3629077