Recursion is nothing more than a function that calls itself.





Program Code :


import java.util.Scanner;
public class FactorialByRecursion{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
System.out.println("Enter a number to findout factorial");
int input=scn.nextInt();
int f=fac(input);
System.out.print(input+" ! = "+f);
}

public static int fac(int a) {

if(a == 1) { return 1;}

return a * fac(a-1);
}
}



Output :

Enter a number to findout factorial
10
10 ! = 3628800


 
Top