Created
November 11, 2012 22:06
-
-
Save marioplumbarius/4056426 to your computer and use it in GitHub Desktop.
Programa em Java que calcula o fatorial de um número inteiro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Faça um programa que calcule o fatorial de um número inteiro. | |
* Repita a operação de cálculo X vezes. | |
*/ | |
package cap14; | |
import java.util.Scanner; | |
public class Exercicio03 { | |
public static void main(String[] args){ | |
Scanner ent = new Scanner(System.in); | |
int num, fat = 1; | |
int cont = 1; | |
do{ | |
System.out.println("Digite um nº"); | |
num = ent.nextInt(); | |
for(int i = 1;i <= num; i++){ | |
fat = fat * i; | |
} | |
System.out.println("!" + num + " = " + fat); | |
cont++; | |
}while(cont < 2); | |
} | |
} | |
/* Passo-a-passo do algoritmo | |
fat = fat * i -> fat | |
fat = 1 * 1 -> 1 | |
fat = 1 * 2 -> 2 | |
fat = 2 * 3 -> 6 | |
fat = 6 * 4 -> 24 | |
fat = 24 * 5 -> 120 | |
*/ |
Valeu, ajudou.
thanks!
Muito obrigado!
Ajudou muito, vlw
Ajudou demais, obrigada!
thank so much mano!!
Por acaso eu acabei aqui lendo seu código e notei que o laço DO...WHILE não é necessário.
Esse laço só vai executar uma vez e pois o contador será igual a 2 após o término do laço.
Removendo a parte do scanner e o contador poderíamos resumir assim:
int num = 2; // Fatorial a ser calculado
int fat = 1;
for(int i = 1; i <= num; i++){
fat = fat * i;
}
System.out.println("!" + num + " = " + fat);
muito bom
me ajudou muito na aula de programação no IFMA
obrigado você me garantil um 10 na melhor materia do mundo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!