Find Factorial of large numbers

Find Factorial of large numbers

You may face the problem of finding factorial of large numbers. Some people can use Long data type but that’s too not solve your problem.

So what could be the solution for it, one solution is the use of Java’s BigInteger class which provides operations analogues to all of Java’s primitive integer operators and for all relevant methods from java.lang.Math.

Now I will show you how to use BigInteger class to find factorials of large numbers.
First you have to import the java.math.*; to use the class BigInteger.
Here in the program we take the value from user and pass it as argument in the function fact().

Inside the fact(),
We declare two objects of BigInteger with initial values of 1.
In for loop we compare the two values ‘i and n’ using the compareTo() method and increment the value of ‘i’ using method add() of BigInteger class.
Multiplication is done using the multiply() method and at last the computed value is returned back to the main function.



Author Description

Amit Gupta

I am great fan of Android and I am always keen to develop different and useful app for people on android. I am always in favor of open source and freeware software and like to work in these type of fields in future. - Follow me @akaamitgupta

No comments yet.

Join the Conversation