Iterativo y recursivo
import java.util.Scanner; public class factorial { public static void main(String[] args) { // TODO Auto-generated method stub Scanner lee = new Scanner(System.in); int n = lee.nextInt(); lee.nextLine(); System.out.println(factotrial_recursion(n)+ " y "+ factorial_iterativo(n)); lee.close(); } public static long factotrial_recursion( int n){ if(n==0){ return 1; }else{ return n*factotrial_recursion(n-1); } } public static long factorial_iterativo (int n){ long sol =1; for (int i = n; i > 0; i--) { sol*=i; } return sol; } } |