-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx05.java
More file actions
37 lines (29 loc) · 947 Bytes
/
Copy pathEx05.java
File metadata and controls
37 lines (29 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Escreva um programa recebe um valor N inteiro e que, se N for positivo, retorna o fatorial de N (N!).
Se não for possível calcular o fatorial, a função deve imprimir uma mensagem e retornar o valor -1.
*/
import java.util.Scanner;
public class Ex05 {
public static int Ex05_fat(){
Scanner leia = new Scanner(System.in);
System.out.println("Digite N: ");
int N = leia.nextInt();
if (N > 0){
int num_inicial = N;
int aux_fat = N;
while(aux_fat > 1){
N = N*(aux_fat-1);
aux_fat--;
}
System.out.println("Fatorial de " +num_inicial+ " eh: " +N);
return N;
}
else{
System.out.println("Impossivel calcular fatorial de " +N);
return -1;
}
}
public static void main(String[] args){
Ex05_fat();
}
}