Coders Packet

Large number factorial calculator in C++

By Chandan Giri

It is a C++ based factorial calculator of large numbers which ae not possible to store in normal data types

In this program we use vector for dynamic memory allocation to store all the values and calculate the factorial of a number which is very large and not possible to store in normal data types like long , int , long long int . So we use vector.

The code is  :

#include<bits/stdc++.h>
using namespace std;
void multiply(int x, vector &temp)
{
int carry = 0;



for (int i=0; i<temp.size(); i++)
{
int product = temp[i] * x + carry;
temp[i] = product % 10;
carry=product/10;
}
while(carry)
{
temp.push_back(carry%10);
carry =carry/10;


}
}
vector factorial(int N){
vector temp;



temp.push_back(1);
int size=1;



for(int x=2;x<=N;x++)
multiply(x, temp);
reverse(temp.begin(),temp.end());
return temp;
}
int main()
{long long int t;
cout<<"Enter the bigger number whose factorial is to be calculated \n";
cin>>t;
vector a;
a=factorial(t);
for(int i=0;i<a.size();i++)
{
cout<<a[i];


}
return 0;
}

 

Download project

Reviews Report

Submitted by Chandan Giri (chandan017)

Download packets of source code on Coders Packet