Header Ads

Learn Together, Stay Connected.

Factorial of 500!

Problem link: UVa 623 - 500!

                                HackerRank - Extra Long Factorial


#include <bits/stdc++.h>
using namespace std;

int main()
{

int n;

while (cin >> n)
{
int arr[1000000];
int carry = 0, k = 0;
arr[0] = 1;

for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= k; ++j)
{
arr[j] = arr[j] * i + carry;
carry = arr[j] / 10;
arr[j] = arr[j] % 10;
}
while (carry)
{
k++;
arr[k] = carry % 10;
carry /= 10;
}
}

cout << n << "!" << endl;
for (int i = k; i >= 0; --i)
cout << arr[i];
cout << endl;
}

return 0;
}

No comments

Theme images by Dizzo. Powered by Blogger.