How to Get Factorial of a Number in Java


Here we will learn how to generate factorial of a given number using recursive function.

Source Code

package com.beginner.examples;

public class FactorialExample {

    public static void main(String[] args){
    	int n = 10;
		System.out.println("Factorial of " + n + " is: " + factorial(n));
	}
    static int factorial(int m)
	{
		if(m <= 1)
			//if 1 return 1
			return 1;
		else
			//else call the same function
			return m * factorial(m-1);
	}
}

Output:

Factorial of 10 is: 3628800
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments