Java Program to find Factorial of number- 7 different ways

Java Program to find Factorial of number- 7 different ways

Are u looking for Java Program to find Factorial of number- 7 different ways?

Java programs are frequently asked in the interview.

In mathematics, the factorial of any number is the multiplication of that number by each number below it till it reaches the last number, which is 1. A factorial is represented by a number and a  ” ! ”  mark at the end. It is widely used in permutations and combinations to calculate the total possible outcomes. The factorial of a non-negative integer is the product of all positive integers less than or equal to that integer

The factorial of a negative number doesn’t exist. And the factorial of 0 is 1.

Formula for Factorial of number:

n! = n * (n-1) * (n-2) * (n-3) * ........ * 1
Example:
  1. 4! = 4*3*2*1 = 24
  2. 5! = 5*4*3*2*1 = 120

In this post, we will learn how to write a program for the factorial of a number in Java using different ways. You will learn about the factorial program in Java with different approaches. Each approach, like factorial program using recursion in Java, offers unique insights into handling factorials, enabling you to choose the most appropriate approach for different possible scenarios and optimize your code accordingly.

1. Java Program to find Factorial of number using for loop:

Program:

import java.io.*;
import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i,fact=1;
System.out.println("Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("The factorial of "+n+ " is " +fact);
}
}

Output:

Enter any number
5
The factorial of 5 is 120

2. Java Program to find Factorial of number using while loop:

Program:

import java.io.*;
import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i=1,fact=1;
System.out.println("Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(i<=n)
{
fact=fact*i;
i++;
}
System.out.println("The factorial of "+n+ " is " +fact);
}
}

Output:

Enter any number
5
The factorial of 5 is 120

3. Java Program to find Factorial of number using do-while loop:

Program:

import java.io.*;
import java.util.*;
class Factorial
{
public static void main(String args[])
{
int i=1,fact=1;
System.out.println("Enter any number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
do
{
fact=fact*i;
i++;
}
while(i<=n);
System.out.println("The factorial of "+n+ " is " +fact);
}
}

Output:

Enter any number
5
The factorial of 5 is 120

4. Java Program to find Factorial of number using Constructor:

Program:

import java.util.*;
class Factorial
{
	int fact=1;
	Factorial(int n)
	{
	    while(n > 0)
                {
                      fact=fact*n;
                      n=n-1;
                }

	}
}
class FactorialNumber
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter any integer");
     int number= sc.nextInt();   
	 Factorial obj =new Factorial(number);
	  System.out.println("The factorial of number is " + obj.fact);      
   }
 }

Output:

Enter any integer
5
The factorial of number is 120

5. Java Program to find Factorial of number using Buffered Reader class:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class FactorialNumber
{
public static void main(String args[]) throws Exception
{
int fact=1;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the n");
int n= Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++)
{
	fact=fact*i;
}
System.out.println("The factorial of number is "+fact);
}
}

Output:

Enter the n 
5
The factorial of number is 120

6. Java Program to find Factorial of number using Command Line Argument:

Program:

import java.util.*;
class FactorialNumber
{
public static void main(String args[])
{
int fact=1;
int n= Integer.parseInt(args[0]);
System.out.println(n);
for(int i=1;i<=n;i++)
{
fact=fact*n;
}
System.out.println("The factorial of number is "+fact);
}
}

Compile:

javac FactorialNumber.java

Run:

java FactorialNumber 5
The factorial of number is 120

7. Java Program to find Factorial of number using Standard value:

Program:

import java.io.*;
class FactorialNumber
{
public static void main(String args[])
{
int fact=1 ;
for(int i=1;i<=5;i++)
{
fact=fact*i;
}
System.out.println("The factorial of number is "+fact);
}
}

Output:

The factorial of number is 120

If your are looking for Core Java Interview Question Free Pdf Download: Please visit the page

:

Leave a Comment