Factorial Program In Java

The multiplication of all positive decreasing integers is called the “factorial of n.” The notation n! represents the factorial of n. To count the possible configurations of “n” objects, this function is put to use. There are n! (N factorial) possible orderings of N objects, according to mathematics. Specifically, consider the following examples:

3! = 3*2*1 = 6

6! = 6*5*4*3*2*1 = 720

The number N! is pronounced as either “N factorial,” “N bang,” or “4 shrieks” in this region. Combinatorics and permutations are typical applications of the factorial. Enroll in the best Java training to learn about the Factorial program and many other important programs in Java under the guidance of subject matter specialists and within the well-built curriculum.

The various factorial implementations in Java are presented below, along with some example results. Check out our handy article if you have no idea how to solve the Factorial in mathematics. We have created five variations of the Factorial program in Java, each with its own unique set of advantages and disadvantages.

Using while loop

Using for loop

Using do while loop

Using method or function

Using recursion

Java Program to Compute the Factorial with Standardized Numbers and Outputs.

Standard values — assume the following code to be universally applicable – alongside example outputs.

class Factorial

{

public static void main(String arg[])

{

int n=4,fact=1;

for(int i=1;i<=n;i++)

{

fact=fact*i;

}

System.out.println(“factoral=”+fact);

}

}

Output

24

Learn Java training in Chennai under the mentorship of subject matter specialists and their immersive training.

Finding Factorial by Java Code Employing For Loop

The for loop: Here is the code using the ‘for loop,’ with output samples marked with #example.

Among the three loops, the for loop is perhaps the most common. For loops are primarily of two types:

for every kind of for loop

standard for loop

class Factorial

{

public static void main(String arg[])

{

long n,fact=1;

Scanner sc=new Scanner(System.in);

System.out.println(“enter number”);

n=sc.nextLong();

for(int i=1;i<=n;i++)

{

fact=fact*i;

}

System.out.println(“fact=”+fact);

}

}

Output

Enter number

5= 120

Java is ruling the IT industry for decades. Learn a Java course in Chennai and have a promising future.

1Finding Factorial by Utilizing Command Line Arguments

Here’s all you need to know to get started with command line arguments in Java.
Java

import java.util.Scanner;

class Factorl

{

public static void main(String args[])

{

long n,fact=1;

n=Long.parseLong(args[0]);

for(int i=1;i<=n;i++)

{

fact=fact*i;

}

System.out.println(“fact=”+fact);

}

}

Output

C:\Users\Kanis\Desktop\E>javac.java

C:\Users\Kanis\Desktop\E>java Factorl 7

factorl=5040

2Finding Factorial Utilizing Function

Java code for factorial calculation using a user-defined method (using function)

import java.util.Scanner;

class Factorl

{

public static void main(String args[])

{

long n,fact=0;

n=Long.parseLong(args[0]);

fact=factCal(n);

System.out.println(“fact=”+fact);

}

static long factCal(long x)

{

long fact=1;

for(int i=1;i<=x;i++)

{

fact=fact*i;

}

return fact;

}

}

Output:

C:\Users\Giri\Desktop\E>javac.java

C:\Users\Giri\Desktop\E>java Factorl 4

factorl=24

Grow big in your career by joining the best Java course in Chennai.

3Finding Factorial Through Recursion

Recursion is the same thing as a function call in and of itself.

import java.util.Scanner;

class Factorl

{

public static void main(String arg[])

{

long n;

Scanner sc=new Scanner(System.in);

System.out.println(“enter number”);

n=sc.nextLong();

long f=Factorl.fact(n);

System.out.println(“factorial=”+f);

}

static long fact(long n)

{

if(n<=0)

return 1;

return Factorl.fact(n-1)*n;

}

}

Output:

Enter number

30

Factorl=8764578968847253504

Enroll in the Java training in Chennai to learn about factorial coding in Java and every other concept in Java.

4Finding Factorial Utilizing a while loop

Here is the syntax for the while loop: The purpose of a while loop is to perform a sequence of statements as much as a certain condition exists.

import java.util.Scanner;

class Factrl

{

public static void main(String arg[])

{

long n,fact=1;

Scanner sc=new Scanner(System.in);

System.out.println(“enter number”);

n=sc.nextLong();

int i=1;

while(i<=n)

{

fact=fact*i;

i++;

}

System.out.println(“fact=”+fact);

}

}

Output:

Enter number

10

fact=3628800

Join the best Java training institute in Chennai and learn about Java from fundamentals to advanced concepts in a well- structured set up.

5Finding Factorial Applying a Do While Loop

In do while, the condition is evaluated during each iteration, whilst in while, the condition is evaluated at the commencement of each iteration.

import java.util.Scanner;

class Fact1

{

public static void main(String arg[])

{

long n,fact=1;

Scanner sc=new Scanner(System.in);

System.out.println(“enter number”);

n=sc.nextLong();

int i=1;

do

{

fact=fact*i;

i++;

}

while(i<=n);

System.out.println(“fact=”+fact);

}

}

Output:

Enter number

7

fact=5040