Factorial of a Number in Java

Factorial of a Number in Java

Here on this page, we will learn how to Find the Factorial of a Number in Java. Factorial is a sequence of a number where we multiply by all previous numbers.

factorial of n (n!) = 1 * 2 * 3 * 4....n


Note : 0! = 1 and 1! = 1

Example : 
5! = 1 * 2 * 3 * 4 * 5 = 120
Factorial of a Number in Java 1

Methods we will discuss

  • Iterative approach for factorial
  • Recursive approach for factorial

Method 1

For an input num

  • Initialize fact = 1
  • If num < 0 : Print Error as we can’t calculate factorial of a negative number
  • Else run a iterative loop in iteration of (i) between [1, num]
  • do fact = fact * i
  • print the value of fact

Java Code

Run
class Main
{
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        int res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
    
    // Driver method
    public static void main(String[] args)
    {
        int num = 6;
        System.out.println("Factorial of " + num + " is " + factorial(num));
    }
}

Output:

Factorial of 6 is 720

Method 2

This method uses the recursive approach, for input num

For an input num

  • Call function getFactorial(num)
  • Set base case when num== 0 return 1
  • Other cases return num * getFactorial(num-1);

Java Code

Run
class Main
{
	// method to find factorial of given number
	static int factorial(int n)
	{
		if (n == 0)
			return 1;
		return n * factorial(n - 1);
	}
	
	// Driver method
	public static void main(String[] args)
	{
		int num = 6;
		System.out.println("Factorial of " + num + " is " + factorial(num));
	}
}

Output:

Factorial of 6 is 720

For similar Questions click on the given button

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

13 comments on “Factorial of a Number in Java”


  • Shital Karek

    public class factorial {
    public static void main(String[] args) {
    int n = 6;
    int fact = 1;

    for(int i = 1 ; i <=n ; i ++)
    {
    fact = fact * i;

    }
    System.out.println(fact + " is thefactorial of" + n);

    }
    }


  • Chetan Mundle

    import java.util.Scanner;

    public class factorial
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter Number : “);
    int num = sc.nextInt();

    int facto = 1;
    int i = num;
    while(i>0) {
    facto *= i;
    i–;
    }

    System.out.println(“Factorial of “+num+” is “+facto);
    }
    }


  • Chetan Mundle

    package prep;

    import java.util.Scanner;

    public class factorial
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter Number : “);
    int num = sc.nextInt();

    int facto = 1;
    int i = num;
    while(i>0) {
    facto *= i;
    i–;
    }

    System.out.println(“Factorial of “+num+” is “+facto);
    }
    }


  • Chetan Mundle

    package prep1;

    import java.util.Scanner;

    public class factorial
    {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter Number : “);
    int num = sc.nextInt();

    int facto = 1;
    int i = num;
    while(i>0) {
    facto *= i;
    i–;
    }

    System.out.println(“Factorial of “+num+” is “+facto);
    }
    }


  • Sornammal

    import java.util.Scanner;
    public class Main{
    public static void main(String[] args)
    {
    System.out.println(” ethe the factoral of numbers “);
    Scanner sc= new Scanner(System.in);
    int n=sc.nextInt();
    System.out.println(factorial(n));
    }
    static int factorial(int n) {
    if (n == 0 || n == 1) {
    return 1;
    }
    return n * factorial(n – 1);
    }
    }


  • Ravi Kant

    public class Factorial {
    public static void main(String[] args) {
    int num=6;
    int factorial=fib(num);
    System.out.println(“”+num+”=”+factorial);
    }
    static int fib(int num){
    if(num>=1)
    return num*fib(num-1);
    else
    return 1;
    }
    }


  • Debyanshu

    import java.util.*;

    public class Main {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int a=in.nextInt();
    factorial(a);

    }
    static void factorial(int a){
    int sum=1;
    for (int i = 1; i <=a ; i++) {
    sum=sum*i;

    }
    System.out.println(sum);
    }

    }


  • Pravin

    public class Factorial_of_a_number {
    public static void main(String[] args) {
    int num = 6;
    int factorial = 1;
    for (int i = 1; i <= num; i++) {
    factorial *= i;
    if (i == num) {
    System.out.println(factorial);
    }
    }
    }
    }


  • Muppidi

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter a number to find its factorial “);
    int n = sc.nextInt();
    int fact =1;
    for(int i=1;i<=n;i++){
    fact = fact*i;
    }
    System.out.println("factorial of given number is : "+fact);
    }
    }


  • drakeshkumard

    import java.util.Scanner;
    public class FactorialofaNumber {
    public static void main(String[] args) {
    System.out.print(“Enter the number :: “);
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    int mul=n, fac=n;
    for (int i=1;i<fac;i++){
    n-=1;
    mul=mul*n;
    }
    System.out.println("Factorial of "+fac+" = "+mul);
    }
    }