-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
26 lines (20 loc) · 758 Bytes
/
Copy pathFactorial.java
File metadata and controls
26 lines (20 loc) · 758 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
/**
* Programa que calcula un factorial de un número, usando un bucle for.
* Por ejemplo, 5! = 5 * 4 * 3 * 2 * 1.
*/
import javax.swing.JOptionPane;
public class Factorial {
public static void main(String[] args) {
/*
* A continuación, se utiliza Long como tipo de dato porque los factoriales
* son números muy grandes. Aún así el tipo Long se quedaría corto muy
* rápido, debiendo utilizar, por ejemplo, la clase BigInteger de la API.
*/
Long resultado = 1L;
int numero = Integer.parseInt(JOptionPane.showInputDialog("Introduce un número, por favor"));
for (int i = numero; i > 0; i--) {
resultado *= i;
}
System.out.println("El factorial de " + numero + " es " + resultado + ".");
}
}