-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.java
More file actions
21 lines (18 loc) · 755 Bytes
/
factorial.java
File metadata and controls
21 lines (18 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
public class factorial {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Introduce un número entero no negativo: ");
int numero = scanner.nextInt();
if (numero < 0) {
System.out.println("El factorial no está definido para números negativos.");
} else {
long factorial = 1; // Inicializamos el resultado en 1
for (int i = 1; i <= numero; i++) {
factorial *= i; // Multiplicamos el resultado por cada número
}
System.out.println("El factorial de " + numero + " es: " + factorial);
}
scanner.close();
}
}