C
I don't usually answer this kind of question, but I can tell you you need it.
advice.Let's get your line code lined up, and then let's talk about a solution that
It works well.Problems in your code#include<iostream>
using namespace std;
The using namespace std; It's a very bad practice. I won't get into it.
that didn't affect you at the moment. But I highly recommend you to investigate
Because it's bad, and you have alternatives.;int num1,resultado,mayor,menor,medio,decena,centena,unidad,calculo;
Here you don't need to start your line with a ;. Besides having all
the statements of the variables in the same line greatly affects the
Code readability.A serious improvement: int num1 = 0;
int resultado = 0;
int mayor = 0;
int menor = 0;
int medio = 0;
int decena = 0;
int centena = 0;
int unidad = 0;
int calculo = 0;
In which the names of the variables are clearly seen. (And note that they are
all initialized, that you do this by default saves you trouble on a
future).But this is still bad, since it's not clear where you're going to use every one.
variable. It's much better to declare them where you use them. (In this we will see more
forward).Then this is a problem in all yours. ifs: if(num1<0)
centena=num1/100 ;
centena=(num1%100)/10 ;
unidad=(num1%100)%10 ;
menor=centena ;
What instructions are those that are inside if? I guess it'll be the whole thing.
block, but that's not clear, and it's not right either.The syntax of the if in C++ is:if (condicion) {
// Codigo
}
And that way you should write them. Still this last one will execute the first
instruction after if if the condition is true. But this one: if(unidad<menor) ;
menor=unidad ;
mayor=centena ;
He'll do nothing right after evaluating the condition.And how can I realize those mistakes? The truth is, you don't have to.
You ask no one, your compiler is able to tell you. I don't know what
compiler uses, so you'll have to google on your own like putting the
alerts at the highest level you use.PD: I already compiled this: if ((num1>100&num1<999)(num1>=-999&num1=<-100))
I guess you wanted to do something like: if ((num1>100 && num1<999) && (num1>=-999 && num1<=-100))
Attention must be paid to the syntax of language.PD2: Please, get used to indenting your code, that will help a lot.
to understand.A baseFor questions of principle, I will not give you a solution that you can
deliver. That would completely eliminate the purpose of leaving you the task,
since you wouldn't learn anything.What if I can leave you is the basis for you to work around them.I don't know how many topics you've dealt with in programming class, your code looks like
I'll just minimum it, so I'll base what you got on it.#include <iostream>
using namespace std;
int main() {
int numero = 0;
cout << "Ingrese un numero de 3 digitos: ";
cin >> numero;
if (numero < 100 || numero > 999) {
cout << numero << "No cumple con los requisitos, vuelva a intentar\n";
return 1;
}
int digito3 = numero % 10;
int digito2 = ((numero % 100) - digito3)/10;
int digito1 = numero / 100; // Como los enteros siempre aproximan hacia
// abajo, para el mas grande se puede hacer
// de esta manera
int mayor = 0;
int menor = 0;
int medio = 0;
// Aqui coloca tu tus comparaciones para ver cual es el mayor, menor y medio
cout << "El numero mas grande que se puede crear es: " << mayor << medio
<< menor << '\n';
return 0;
}
A solution to studyThis solution and no joke accepts it, as if you introduce it, it will be clear that
the copy of the Internet. I don't put it here for that, but because I think so.
You read it, and you're looking on the Internet for everything, you might learn
something that's useful to you.#include <iostream>
#include <array>
#include <algorithm>
#include <cstring>
// Las funciones son muy bunenas para organizar tu codigo
int to_digit(char c) {
return c - '0';
}
int main(void) {
constexpr size_t cantidad_digitos = 3; // Siempre es buena idea utilizar
// constantes en lugar de numeros
// magicos
// Busca lo que es un array. Es mucho mejor para guardar colecciones de
// datos
std::array<int, cantidad_digitos> digitos{};
std::cout << "Ingrese un numero de " << cantidad_digitos << " digitos: ";
for (size_t i = 0; i < cantidad_digitos; ++i) {
char c = '\0';
std::cin >> c;
digitos[i] = to_digit(c);
}
// Los algoritmos que trae la libreria estandar te haran la vida mucho mas
// facil.
std::sort(digitos.rbegin(), digitos.rend());
std::cout << "El mayor numero que se puede formar es: ";
for (int digito : digitos) { // Este for es mucho mas bonito
std::cout << digito;
}
}
If you want to investigate the topics without using that code, they'll be:for loopFunctionsArraysConstant and magical numbersSTL.