Find the factorial of 100 in C++?

Find the factorial of 100 in C++?

the only option is to use string and basic mathematical operations as no data type can store that huge number .

1 Like

would u plz code it…

use an array of integer and implement in such a way that this array can store huge data

I am giving the code for implementing this type of array

The Formula of fact(n) is n*(n-1)(n-2)…321

means you have to multiply and this will give you a huge number that you cannot store in any data type.

              int a[170];
              a[0]=1;
              m=1;
              temp = 0;
              for(i=1;i<=n;i++)  // to multiply n number 
              {
                               for(j=0;j<m;j++)
                               {
                                               x = a[j]*i+temp;
                                               a[j]=x%10;
                                               temp = x/10;
                               }
                               while(temp!=0)
                               { 
                                 a[m]=temp%10;
                                 temp = temp/10;
                                 m++;
                               }
              }

Now print the array element from m-1 to 0 index;

You can also follow my code for [this][1] problem

My code is :

[http://www.codechef.com/viewsolution/1567814][2]

If you still have any doubt in this method you may ask me.

Happy Coding!!!
[1]: http://www.codechef.com/problems/FCTRL2/
[2]: http://www.codechef.com/viewsolution/1567814

6 Likes

@alokamaster : i would rather advise you to try this as an assignment. you will definitely learn the art of dealing with operations of large numbers…

secondly, you will get introduced to the concept of polynomial multiplications and how to use Fourier transform to do the same!!

1 Like

Have you tried http://www.codechef.com/problems/FCTRL2? If you’re still having difficulty with understanding the approaches outlined in the previous answers, go to the tutorial of the aforementioned problem, where a detailed and very well written explanation of roughly the same approach can be found.

learn python

C++ doesn’t data types to store such large values as 100! will have approx 150 digits, where as long long int support only 4*10^18. So, you need to handle big integers with the help of arrays…U can take help from this link on how to handle large numbers…! “http://e-maxx.ru/algo/big_integer”…

1 Like

You can use c++ boost library for multiplication of very large numbers… to find 100! use boost library for large multiplication…

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;

mp::cpp_int fact(mp::cpp_int a)
{
	if(a==1)
		return 1;
	if(a==0)
		return 0;
	return a * fact(a-1);
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		mp::cpp_int a;
		cin>>a;
		cout<<fact(a)<<endl;
	}
	return 0;
}

actually this is solution to FCTRL2 problem…

1 Like

Using Boost Library https://changeblogger.org/factorial-of-large-number-in-cpp/