Skip to content

Instantly share code, notes, and snippets.

@lnrsoft
Created November 19, 2015 20:40
Show Gist options
  • Save lnrsoft/943a0f37bc71c4b80b72 to your computer and use it in GitHub Desktop.
Save lnrsoft/943a0f37bc71c4b80b72 to your computer and use it in GitHub Desktop.
You are given an integer n. Determine the factorial of this number.
#include <vector>
#include <iostream>
int main()
{
std::vector<int> factorials;
factorials.push_back(1);
int n;
std::cin >> n;
for (auto i = 1; i <= n; i++)
{
for (unsigned int j = 0; j < factorials.size(); j++)
{
factorials[j] *= i;
}
for (unsigned int j = 0; j < factorials.size() - 1; j++)
{
factorials[j + 1] += factorials[j] / 10;
factorials[j] %= 10;
}
while (factorials[factorials.size() - 1] / 10)
{
factorials.push_back(factorials[factorials.size() - 1] / 10);
factorials[factorials.size() - 2] %= 10;
}
}
for (int i = factorials.size() - 1; i >= 0; i--)
{
std::cout << factorials[i];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment