Factorial program in java

Let’s learn factorial program in java.

Factorial program in java

Find the factorial of a number is one of the common java interview question.

Factorial is nothing but the product of all numbers less than or equal to given number. Denoted as n! For example,

factorial program in java

6! = 6 * 5 * 4 * 3 * 2 * 1 = 720 (6! means 6 factorial)

4! = 4 * 3 * 2 * 1 = 24 (4! means 4 factorial)

In finding factorial of a number in java there are many ways. Below are few ways to write factorial program in java.

  1. factorial program using loop.
  2. factorial program using recursion or recursive.
  3. factorial program using while loop.
  4. factorial program using do-while loop.

Also read – write a java program to check armstrong number

Now let’s learn to find the factorial of a number using loop in java. Here’s the java program,

public class FactorialDemo
{
   public static void main(String[] args)
   {
      int number = 6, factorial = 1;
      for(int a = 1; a <= number; a++)
      {
         factorial = factorial * a;
      }
      System.out.println("Factorial of " + number + " is : " + factorial);
   }
}


Output:

Factorial of 6 is : 720


Factorial program using recursion or recursive

Similarly let’s learn to find the factorial program using recursion or recursive.

public class FactorialRecursion
{
   public static void main(String[] args)
   {
      int factorial = 1;
      int number = 6;
      factorial = factorialFunction(number);
      System.out.println("Factorial of " + number + " is : " + factorial);
   }
   static int factorialFunction(int num)
   {
      if(num == 0)
      {
         return 1;
      }
      else
      {
         return(num * factorialFunction(num - 1));
      }
   }
}


Output:

Factorial of 6 is : 720


Factorial program using while loop

Also read – fibonacci program in java

Here’s factorial program using while loop.

import java.util.Scanner;
public class FactorialUsingWhileLoop
{
   public static void main(String[] args)
   {
      int number, factorial = 1;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number : ");
      number = sc.nextInt();
      int a = 1;
      while(a <= number)
      {
         factorial = factorial * a;
         a++;
      }
      System.out.println("Factorial of " + number + " is : " + factorial);
      sc.close();
   }
}


Output:

Please enter a number : 5
Factorial of 6 is : 120


Factorial program using do while loop

Meanwhile let’s learn factorial program using do while loop. Here’s the java program to find factorial of a number.

import java.util.Scanner;
public class FactorialDoWhileLoop
{
   public static void main(String[] args)
   {
      int number, factorial = 1;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number : ");
      number = sc.nextInt();
      int a = 1;
      do
      {
         factorial = factorial * a;
         a++;
      }
      while(a <= number);
      System.out.println("Factorial of " + number + " is : " + factorial);
      sc.close();
   }
}


Output:

Please enter a number : 7
Factorial of 7 is : 5040


factorial from 1 to 10 in java

Now let’s learn factorial from 1 to 10 in java.

public class FactorialFrom1To10
{
   public static void main(String[] args)
   {
      int count;
      long factorial = 1;
      System.out.printf("%4s%30s\n", "Number", "Factorials");
      for(count = 1; count <= 10; count++)
      {
         factorial *= count;
         System.out.printf("%4d%,30d\n", count, factorial);
      }
   }
}


Output:

Number Factorials
1 1
2 2
3 6
4 24
5 120
6 720
7 5,040
8 40,320
9 362,880
10 3,628,800


reverse factorial program in java

Here’s reverse factorial program.

import java.util.Scanner;
public class ReverseFactorialProgram
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to compute reverse factorial: ");
      int number = sc.nextInt();
      int reverse = number;
      for(int a = 2; reverse != a; a++)
      {
         reverse = reverse / a;
      }
      System.out.println("Reverse factorial of " + number + " is: " + reverse);
      sc.close();
   }
}


Output:

Please enter a number to compute reverse factorial: 720
Reverse factorial of 720 is: 6