Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program to get factorial of any integer. #645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Algorithms/Maths/factorial/FactorialBigIntegers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
#define MAX 500
using namespace std;
int multiply(int x, int res[], int res_size);
void extraLongFactorials(int n) {
int res[MAX];
res[0] = 1;
int res_size = 1;
for (int x=2; x<=n; x++)
res_size = multiply(x, res, res_size);
for (int i=res_size-1; i>=0; i--)
cout << res[i];
}
int multiply(int x, int res[], int res_size)
{
int carry = 0;
for (int i=0; i<res_size; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod/10;
}
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
int main()
{
int n;
cout<<"Enter the number to get factorial";
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
extraLongFactorials(n);
return 0;
}