The first thing you need to keep in mind is that the loop (para) needs to be reversed. This is because it will become easier to concatenate the multiplications into one string (type cadeia).
For example, the factorial of 3 has resulted 6 the representation would be 3! = 3x2x1 = 6. Note that it is much easier to concatenate the text if the multiplication starts with 3.
With this in mind, follow the algorithm
inteiro numero, fatorial, resultado = 1
cadeia texto = "" //Variavel para salvar a representação final (3x2x1)
escreva ("Insira um número a ser fatorado: ")
leia (numero)
/* Perceba que aqui o loop (para), corre de trás pra frente. Ou seja, ele começa no número
* que foi digitado pelo usuário (fatorial = numero), acontece enquanto o multiplicador
* for maior ou igual a 1 (fatorial >= 1) e vai diminuindo 1 do multiplicador
* a cada passada (fatorial--) */
para (fatorial = numero; fatorial >= 1; fatorial--)
{
// Aqui, se for 1 não precisamos concatenar o sinal de multiplicação (x)
se(fatorial == 1){
texto = texto + fatorial
}senao{
texto = texto + fatorial + "x"
}
resultado = resultado * fatorial
}
escreva (numero, "! = ", texto, " = ", resultado)
Boy that’s right! I need to improve my logic! I had already given up on this algorithm! Vlw jbueno!
– Sandson Costa
@Sandsoncosta For nothing. Whenever you need can post here, sometimes it takes a while, but questions well asked will always be answered.
– Jéf Bueno