Tenho uma duvida bem simples, estou fazendo um exercício onde é pedido q eu calcule o fatorial de um numero, até ai td bem, e escreva o na tela, além do resultado, o calculo feito. como disse sou iniciante e não consegui pensar em como fazer isso
segue abaixo o que eu consegui fazer:
public static void main(String[] args) {
Scanner tec = new Scanner(System.in);
System.out.print("Digite um numero :");
int n = Integer.parseInt(tec.nextLine());
int f = 1;
int c = n;
while(c>0){
f = f*c;
c--;
}
System.out.println("O fatorial de "+ n +" é "+ f);
}
Se for pra imprimir em ordem crescente é + pratico um for.
public static void main(String[] args) {
Scanner tec = new Scanner(System.in);
System.out.print("Digite um numero :");
int n = Integer.parseInt(tec.nextLine());
int f = 1;
for (int c = 1; c <= n; c++) {
System.out.println("f * c = " + (f *= c));
}
System.out.println("O fatorial de " + n + " é " + f);
}
vlw, ajudou bastante as dicas.
eu queria deixar algo tipo 1x2x3x4…=x , dai consegui fazer com o for criando mais uma varavel. ficou meio feio e deve ter algum jeito mais facil mas funcionou hahaha,
segue o codigo aqui, vlw
Scanner tec = new Scanner(System.in);
System.out.print("Digite um numero :");
int n = Integer.parseInt(tec.nextLine());
int f = 1;
int j;
for(int c = 1; c<= n; c++){
j =(f *= c);
if(c<n){
System.out.print(j+"x");
}else{
System.out.print("=");
}
}
System.out.println(f);
System.out.println("O fatorial de "+ n +" é "+ f);
}