Created
November 19, 2015 20:40
-
-
Save lnrsoft/943a0f37bc71c4b80b72 to your computer and use it in GitHub Desktop.
You are given an integer n. Determine the factorial of this number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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