/* ======================================================== The classic factorial function, but rewritten as a "devide and conquer" algorithm ======================================================== */ import java.util.Scanner; public class Fac { static int Fac(int N) { int helpSol; int mySol; /* --------------------------- Base cases --------------------------- */ if ( N == 1 ) { mySol = 1; } else { helpSol = Fac( N - 1 ); // Tell someone to solve a smaller problem mySol = N * helpSol; // Use the solution of the smaller problem // to solve my (original) problem } return(mySol); } public static void main(String[] args) { Scanner in = new Scanner( System.in ); System.out.print("N = "); int N = in.nextInt(); int sol = Fac( N ); System.out.println("\n\nSolution:" + sol); System.out.println("======================="); } }