public class EjemploFactorial {
public static void main(String[] args) {
int factorial = 1;
for (int i = 1; i < 11; i++) {
factorial *= i;
System.out.println("Factorial de " + i + " = " + factorial);
}
}
}
public class EjemploFactorial {
public static void main(String[] args) {
int factorial = 1;
for (int i = 1; i < 11; i++) {
factorial *= i;
System.out.println("Factorial de " + i + " = " + factorial);
}
}
}
Excelente sin embargo consumiria mucha memoria si el ciclo for tendria un n>100 Por ello es bueno usar en esos casos recursividad
import java.util.Scanner;
public class FactorialRecursivo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Introduce un número: ");
int num = sc.nextInt();
sc.close();
System.out.println("\nEl factorial de " + num + " es: " + factorial(num));
}
public static int factorial(int num){
if(num == 0){
return 1;
}
else
return num * factorial(num-1);
}
}