Como fazer fatorial com while?

6 respostas
P

Como?

6 Respostas

aix

Ola @Pixels15,

conhece o Guava?

tem método para calcular o fatorial, ex:

BigInteger fatorial = BigIntegerMath.factorial(new BigInteger("12").intValue());

Library : Guava
Jar File: guava-19.0.jar

P

Mas queria o método mais simples usando while…

aix

while é apenas uma estrutura de repetição, não esta associado ao cálculo.

elton.alex
import javax.swing.JOptionPane;

public class FatorialWhile {
public static void main(String[] args) {
	int intcontador = 1, fat, valor;
	valor = Integer.parseInt(JOptionPane.showInputDialog(null,"Entre numero", "entrada", JOptionPane.INFORMATION_MESSAGE));
	fat = 1;
	String result = "o fatorial de " + valor + " é :";
	while (intcontador <= valor) {
		fat = fat + (fat * (valor - 1) );
		valor--;
	}
	JOptionPane.showMessageDialog(null, result + fat, "Resultado", JOptionPane.INFORMATION_MESSAGE);
}
}

Origem: Fatorial while "help".[Resolvido]

aix

legal, mas fatorial acima de 12 ele se perde e acima da resultado 0 precisa ajustar dependendo da utilização.

cviniciusm

Olá,

algoritmo:

fat=1
n=5
while(n > 1) {
   fat = fat*n;
   n--;
}
imprime fat

Isso é muito trivial, tente escrever o algoritmo primeiro, como fiz, para então tentar a implementação.

Criado 22 de setembro de 2016
Ultima resposta 25 de set. de 2016
Respostas 6
Participantes 4