Recursion: Java program to calculate factorial of a number by recursive method

Recursion is the process of calling a method by itself. Recursion is a slow process as compared to its iterative equivalent. However, you can create a simple and easy to understand program with the help of recursion.  Factorial of a particular number is product of all numbers between 1 and the given number. Here is a Java program to calculate a factorial of a number by recursive method:

class Factorial {

int fac(int x) {

int res;

if(x==1) return 1;

res = fac(x-1) * x;

return res;



class Recursion {

public static void main(String args[ ]) {

Factorial f = new Factorial();

System.out.println(“Factorial of 2 is ” + f.fac(3));

System.out.println(“Factorial of 3 is ” + f.fac(4));




Factorial of 2 is 2

Factorial of 3 is 6

Leave a Reply