Java Program to Find the Factorial of a Number
Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
Below questions may asked by interviewer
Q) So, basically what is factorial?
Ans: A factorial is a function that multiplies number by every number. For example 4!= 4*3*2*1=24. The function is used, among other things, to find the number of ways “n” objects can be arranged.
Q) How to calculate?
For example :
Consider :
- 2! = 2 x 1 = 2
The possibility of 2! is two ways like {2,1}, { 1,2 }.
Same as like :
4! = 4 x 3 x 2 x 1 = 24.
- 24 = the arrangement of 4! is {1,2,3,4}, {2,1,3,4}, {2,3,1,4}, {2,3,4,1}, {1,3,2,4}, etc.
Same as like 5! , 10! , N!.
The following program has been written in 5 different ways, using while loop, for loop, do while loop, using method.
Now, let’s get into the programming part.
1. Java Program To Calculate Factorial using standard values with outputs
/*
* Author: Zameer Ali
* */
class Factoral {
public static void main(String arg[]) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
System.out.println("factoral=" + fact);
}
}
Output:
factorial =120
2. Find Factorial Using For Loop getting user input
/*
* Author: Zameer Ali
* */
import java.util.Scanner;
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
7=5040
3. Find Factorial using Function
/*
* Author: Zameer Ali
* */
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\ZameerAli\Desktop\E>javac.java
C:\Users\ZameerAli\Desktop\E>java Factorl 10
factorl=3628800
4. Find Factorial Using Recursion
Recursion: A Recursion is a function call itself – you can check out more information about what is recursion in java here?
/*
* Author: Zameer Ali
* */
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
5. Find Factorial Using while loop and do while loop
/*
* Author: Zameer Ali
* */
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
Do while loop example
/*
* Author: Zameer Ali
* */
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:
output:
enter number
7
fact=5040