Métodos 3
This commit is contained in:
1 parent
75faea13e0
commit
35b63ac2b1
3 files changed
+22
-2
No files matched your search
+20
-1
@@ -1,4 +1,23 @@
|
||||
package PACKAGE_NAME;
|
||||
import java.util.*;
|
||||
|
||||
public class Metodos3 {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
boolean detener = false;
|
||||
int num = 0;
|
||||
while(!detener){
|
||||
System.out.println("Introduce un numero: ");
|
||||
num = sc.nextInt();
|
||||
if(num < 0) {
|
||||
System.out.println("Tiene que ser un numero positivo");
|
||||
} else detener = true;
|
||||
}
|
||||
System.out.printf("El factorial de %d es %.2f\n", num, factorial(num));
|
||||
sc.close();
|
||||
}
|
||||
private static double factorial(int n) {
|
||||
double result = 1.00;
|
||||
while(n > 0) result *= (double)n--;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user