by Dinesh Thakur Category: Data Type

This JavaFactorialUsingRecursion Example shows shows how to generate factorial of a given number using recursive function. Recursive are the function which calling method itself. the java recursion factorial example demonstrate recursive function call to find factorial. In mathematics, factorial is defined as n! = n*(n-1)...2*1. e.g. If we want to calculate factorial of 10 , then its factorial value is (10*9*8*7*6*5*4*3*2*1) = 3628800.


Here is the Java Example for the program JavaFactorialUsingRecursion : 

import java.util.Scanner;
public class JavaFactorialUsingRecursion
             static int fact(int a)
                 if(a <= 1)
                   return 1;
                   return a * fact(a-1);
public static void main(String args[])
             System.out.println("Enter a number to find factorial");
             Scanner scan = new Scanner(;
             int n = scan.nextInt();
             int Factorial = fact(n);
             System.out.println("Calculated Factorial of the number is: " + Factorial);
             catch (Exception e){}

Output of program

Java Factorial Using Recursion Example


































When, the method fact(5) is invoked, the value 5 will be assigned to parameter a. It will return :


When fact(4) is called with the argument 4, the function fact is re-entered and a new set of local variables and parameters is allocated. This way, we have a different set of local variables and parameters for each call of the function. While referencing, only the most recent copy of these variables can be referenced. When a return from fact takes place, the most recent allocation of these variables is freed, and the previous copy is reactivated.

To manage these different set of variables and parameters, stack is used. So 5*fact(4) is placed on the stack.

When fact(4) is called, a new set of variables is created i.e. a new variable a is created and is assigned value 4. It will call 4*fact(3).The call to fact(3) will force 4*fact(3) to be pushed on the stack above 5*fact(4):

When fact(3) is called, a new variable a is created and is assigned value 3. It will push 3*fact(2) onto the stack and call to the function fact is placed with the parameter value 2.

When fact(2) is called, the value 2 is assigned to a. It will push 2*fact(1) onto the stack

When fact(l) is called, the value 1 is assigned to a. It will return 1.

When the function returns, the stack is popped i.e. the top allocation is freed and the previous allocation becomes the current stack top.

Itcan be shown as :






Hence combining all will return 5*4*3*2*1 i.e. 120

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content