let's start Code

Large Number Factorial

>> Large Number Factorial:


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 10000000
#define MAX 500


int multiply(int x, int res[], int res_size);

void factorial(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);
    }
    //cout << "factorial of Given NUmber is \n";
    for(int i = res_size - 1; i >= 0; i--)
        cout << res[i];
    cout << endl;
}

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()
{
    FastIO;
  #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  #endif

    int n;
    cin >> n;
    while(n--){
        int n;
        cin >> n;
        factorial(n);
    }
    return 0;
}
Share:

No comments:

Post a Comment

About

let's start CODE

Popular Posts