Factorial of large number
- This program shows the concept to find factorial of large number.As it is not possible for simple datatype to store more than 100’s of digit.Integer and long are note able to store such a long numbers.
- To store large numbers of digits java introduce a new class called import java.math.BigIntege.This class helps to store large number of digits.
- So it is easy to find factorial of large number using this class.Users can not use a * operator to multiply two numbers.
- multiply keyword is used to multiply two numbers.
- We have made a object b1 of class BigInteger.
- “b1” will store a factorial of a input number.
- BigInteger.valueOf(i) will convert the “int” value into “BigInteger”.
- b1.multiply(BigInteger.valueOf(i)) is used to multiply the value of “i” with “b1”.
Working of program
public static void main(String arg)
BigInteger b1=new BigInteger("1");
Scanner kb=new Scanner(System.in);
System.out.println("Enter number to find factorial:- ");
b1 = b1.multiply(BigInteger.valueOf(i));
What if we use a * operator?
As in a picture we can see that program will give error of “operator * cannot applied to import java.math.BigInteger”.