Factorial Program in Java

Sharing is healthy !!

Hey guys!! Let’s move ahead and learn factorial program in java using loop, that is, for loop.

Earlier you learnt above mentioned program using recursion and also its definition.

Also Read – Factorial Using Recursion In Java

Here in this post scanner class is used and imported from java.util package. So, let’s see code implementation,

example: Factorial Program in Java

Let’s see an example on factorial of a given number in java,

import java.util.Scanner;

public class FactorialProgram
{
   public static void main(String[] args)
   {
      int x;
      int y;
      int factorial = 1;

      System.out.println("Please enter an integer to find its factorial: ");
      Scanner sc = new Scanner(System.in);
      x = sc.nextInt();

      if(x < 0)
      {
         System.out.println("Only positive numbers should be entered");
      }
      else
      {
         for(y = 1; y <= x; y++) // For loop starts here
            factorial = factorial * y;
            System.out.println("Factorial of " + x + " is = " + factorial);
      }
   }
}

Output:

Factorial Program in Java


using standard values

Here let’s see factorial in java using standard values,

public class FacorialProgram
{
   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 for loop java

Now let’s learn factorial program in java using for loop,

import java.util.Scanner;

public class FactorialForLoop
{
   public static void main(String[] args)
   {
      long number, factorial = 1; 
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter an integer to find its factorial : "); 
      number = sc.nextLong();

      for(int a = 1; a <= number; a++)
      {
         factorial = factorial * a;
      }
      System.out.println("Factorial of " + number + " is : " + factorial);
      sc.close();
   }
}

Output:

Please enter an integer to find its factorial : 5
Factorial of 5 is : 120


factorial function java

Let’s learn how to find factorial in java using factorial function,

import java.util.Scanner;

public class FactorialFunction
{
   public static void main(String[] args)
   {
      long number, factorial = 0; 
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to find its factorial : "); 
      number = sc.nextLong();
      factorial = functionFactorial(number); 
      System.out.println("Factorial of " + number + " is : " + factorial);
      sc.close();
   }
 
   static long functionFactorial(long n)
   {
      long fact = 1;
      for(int a = 1; a <= n; a++)
      { 
         fact = fact * a;
      }
      return fact;
   }
}

Output:

Please enter a number to find its factorial : 6
Factorial of 6 is : 720


factorial program using recursion in java

Let’s learn factorial using recursion in java. We can execute factorial program in two ways,

// using scanner class

import java.util.Scanner;

public class FactorialRecursion
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to find its factorial : "); 
      int number = sc.nextInt();
      // calling the user defined function
      int factorial = factorialFunction(number);
      System.out.println("Factorial of " + number + " is : " + factorial);
      sc.close();
   }
 
   static int factorialFunction(int n)
   {
      int result;
      if(n == 1)
      {
         return 1;
      }
      // here we are calling the recursion function
      result = factorialFunction(n - 1) * n;
      return result;
   }
}

Output:

Please enter a number to find its factorial : 10
Factorial of 10 is : 3628800


// without using scanner class

public class RecursionFactorial
{
   public static void main(String[] args)
   {
      int factorial = factorialFunction(6);
      System.out.println("Factorial of 6 is : " + factorial);
   }
 
   static int factorialFunction(int n)
   {
      int result;
      if(n == 1)
      {
         return 1;
      }
      // here we are calling the recursion function
      result = factorialFunction(n - 1) * n;
      return result;
   }
}

Output:

Factorial of 6 is : 720


factorial while loop java

Now we are going to learn factorial program in java using while loop,

import java.util.Scanner;

public class FactorialWhile
{
   public static void main(String[] args)
   {
      int number, factorial = 1; 
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to find its factorial : "); 
      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 to find its factorial : 6
Factorial of 6 is : 720


using do while loop

Let’s see an example on factorial program in java using do while loop,

import java.util.Scanner;

public class FactorialDoWhile
{
   public static void main(String[] args)
   {
      int number, factorial = 1; 
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number to find its factorial : "); 
      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 to find its factorial : 7
Factorial of 7 is : 5040


conclusion

That’s it guys. This is all about how to find factorial in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Sharing is healthy !!

You May Also Like