How to find factorial of any number in java..

VAIBHAV NIRMAL
2 min readJan 1, 2022

Do you know 100! =??

We know finding factorial of number greater than 20 is really difficult..

with the help of java program it becomes very easy. Lest understand how?

Lets see the program:

Output:

Progarm:

//You can find factorial of any numberimport java.math.BigInteger;
import java.util.Scanner;
public class factorialUsingBigInteger {
public static String factorial(int num){
BigInteger f= new BigInteger("1");
for (int i = 2; i <= num; i++) {
BigInteger n = BigInteger.valueOf(i);
f=f.multiply(n);
}
return f.toString();
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int num= sc.nextInt();
System.out.println("Factorial of given number is : " + factorial(num));
}
}

BigInteger In Java

What is a BigInteger in Java?

BigInteger provides analogues to all of Java’s primitive integer operators, and all relevant methods from java. lang. Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

What is the range of BigInteger?

BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger . The range must be at least 1 to 2500000000.

Learn about BigInteger:

Java Docs

--

--

VAIBHAV NIRMAL

Hi there.. This is Vaibhav. A Passionate Software Developer. I'm all about creating cool stuff with code. I love React.js, Angular, and Spring Boot.