2
Sep

# Como Invertir un Numero en C++ – Ejercicios Resueltos 17

Well then we came with another exercise more in this case we have to develop an algorithm that we give a number and we have to turn around and have to put reverse treating it as number not as an array in which we enter data in different positions or as a text string to turn or trap like that treating him as number okey develop some ability to generate algorithm this will be very interesting when we see Recursive in the future so we’ll want to be getting yourself would be great but we will see how it is going to ask for no good here save work instead of asking the user directly we will create a variable called num and here we put this number for example any number and the following we will see how we would upside will say the following will put the number is greater than or equal that 10 then we will make the following screen will show for forgiveness result will display on screen a number that will be the result the remainder of dividing the number 10 if you do not understand now what I explain and then divide the number by itself for 10 ie I will divide put this so that they may see you better that is divided by 10 and the remainder will be this because that’s what I’ll show you later remove this number by dividing 10 likewise divide it between 10 and so forth do so at the end to show me this number that I’m realizing what I am me realizing is that this number then the first thing will not go to see the show launched and are backwards saw as 4531986 but 4 did not show it then we would have made them here would have to return the printf number so we will try to do the following mode with a do while and there do not for a while I was not mistaken then is simply that here again we must reprinting the number we have left and there already be complete hey gives me a compile error it gives me a compile error and what is happening there ladies if gentlemen is of course the key you clear now if it works good and it does not matter how many we put there because you see it totally reverses that there can compare well since it is already that’s all print number will be good when we do so Recursive okey if this has cost you this generate work This algorithm has cost you work is not known very well how to do it still needs to do more basic exercises for that is because like all move dale

• Maverick Dark X says:

Este tipo de ejercicios matematicos si que me cuestan . No sé nada de C++ ni de C, pero creo que estos ejercicios son universales para cualquier lenguaje de programacion, uso mas php que otra cosa.
¿Como puedo conseguir mas destreza en este tipo de ejercicios?
Por cierto, tienes mi like
Gracias.

• Rodrigo Cruz Magne says:

muy buen ejercicio, pero si queremos el resultado como numero? como colocaste a num = 123456 entonces el numero seria numinv=654321

• Alex Trip says:

Si la condición la cambias por num >= 0 si saldría el primer dígito sin necesidad de poner el segundo printf

• Alejandro Marin says:

hola puedes invertir estos datos 19800715|robert|sanchez, que te los han dado en un archivo .txt

• Benjamín Söderlund Valencia says:

como sería si quiero hacer una función que haga eso ? además usando "using namespace std;"?

• Sebastian Quiroz says:

Excelentes Vídeos, Sirven demasiado, son básicos pero que nos orientan a llevarlos a gran escala, de verdad Muchas Gracias!

• Ismael Aguilar says:

gracias

• Luis Hernán Mesajil López says:

int invertir_cifras( int numero){

int invertido = 0;
while ( numero != 0){

invertido = invertido*10 + numero%10;
numero = numero/10;
}

return invertido;
}

• Jairo Jimenez says:

Excelente explicacion! muchas gracias Javier

• carlos sebastian tzab chi says:

public static void main(String[] args) {
// TODO code application logic here
int num=4324321;

while(num>=1){
System.out.println(num%10);
num /=10;
}

}

• Joel V says:

#include <iostream>
using namespace std;

int main() {
int leNum=472,leRes;

for(int i=0;i<3;i++){
leRes=leNum%10;
leNum/=10;
cout<<leRes;
}
return 0;
}

o

#include <iostream>
#include <string>
using namespace std;

int main() {
char leNum1="1234";
char leNum2;
int leCan,l=0;
leCan=strlen(leNum1);
for(int i=leCan;i>0;i–){
leNum2[l]=leNum1[i-1];
cout<<leNum2[l];l++;
}
cin.get();
return 0;
}

• Pedro Guzman says:

Yo lo hice en Java y me quedo asi:

public static void main(String[] args) {
Scanner Scan=new Scanner(System.in);

long num, inv=0;

System.out.println("Dame el numero a invertir");
num=Scan.nextInt();

for(int i=1;num>0;i++){
inv=(inv+num%10)*10;
num=(num-num%10)/10;
}
System.out.println("El inverso es "+inv/10);
}