Header Ads Widget

Trending

6/recent/ticker-posts

Factorial of a large number

Find Factorial of a Large Number

Getting Factorial of a large might be a tough task in other languages like c++, but java offers a big Integer class that can handle very large numbers. As Primitive data of 4 Byte can store only 2147483647. For calculations larger number than it, we will require a Bigger class than it. so java provides BigInteger class that can handle it properly.
BigInteger is Immutable in Java Means You cannot change the value once assigned to BigInteger Variable.

Factorial of a large number using BigInteger in Java



 class Courpedia {

 public static void main (String[] args) 
    {
 Scanner sc = new Scanner(System.in);
  int t= sc.nextInt();
 for(int i=0;i<t;i++)
        {
BigInteger fact= new BigInteger("1");
      long n=sc.nextLong();
      while(n>0)
            {
fact=  fact.multiply(BigInteger.valueOf(n));
               n--;
            }
            
System.out.println(fact);
            
        }
    }
}



Big Integer class in java handles these large numbers very easily. It has various functions which we will discuss in our further blogs.
Now let us see how To get factorial of a large number in C++.

C++ does not have a Class like BigInteger as in Java. so we have to find the factorial of a large number using a different approach


 Factorial of a large number in C++


#include<iostream>
using namespace std;
void main() {
int T;
    cin>>T;
    while(T--) {
        int a[200];
        int rem;
        int i=0;
        int n,m=0;
        int temp;
        cin>>n;
        temp = n; 
 
        while(temp!=0
         {
            rem = temp%10;
            a[i] = rem;
            temp = temp/10;
            i++;  
             m++;
        } 
 
         int h,x=0,ind=0;
         for(i=2;i<n;i++)  
         {
             h = 0;
         for(ind=0;ind<m;ind++)
              {
                 x = a[ind]*i + h;
                 a[ind] = x%10;
                 h = x/10;
              }
  
              while(h!=0
              {
                  a[ind] = h % 10;
                  h = h/10;
                  ind++;
                  m++;
               }
            }
 
            for(i=m-1;i>=0;i--)
             {
                 cout<<a[i];
             }
                cout<<"\n";
            }
}




 You can compare the difference between the number of lines of code in Java solution and in C++ Solution.
here in c++, we are making an array to store all digits of the large number between 0 to 9.

Let us see how we can find the factorial of a large number in Python.
We don't have to bother about Large numbers in Python because it handles large numbers easily. Here is the code to find the factorial of the large numbers in Python.

Factorial of a large number in Python

##shortest solution in Python

T = int(input())
for i in range(T):
    n = int(input())
    fact = 1
    for i in range(1,n+1):
        fact = fact * i
    print(fact)


There are Lots of other ways to do this you can try your own ways that may be more efficient than it. 

Practice this problem on geeks for geeks
Practice this problem on Codechef

 
-: You Might Like:-