If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Calculating the factorial of a large number can lead to very large results, which may not fit into standard data types like
int
orlong long
. To handle factorials of large numbers, you can use techniques like iterative multiplication or storing the digits of the factorial in an array.Here's a C++ code using an array to calculate the factorial of a large number:
cppCopy code
#include <iostream> #include <vector> void multiply(std::vector<int>& result, int x) { int carry = 0; for (int i = 0; i < result.size(); i++) { int product = result[i] * x + carry; result[i] = product % 10; carry = product / 10; } while (carry) { result.push_back(carry % 10); carry /= 10; } } void factorial(int n) { std::vector<int> result; result.push_back(1); for (int i = 2; i <= n; i++) { multiply(result, i); } std::cout << "Factorial of " << n << " is: "; for (int i = result.size() - 1; i >= 0; i--) { std::cout << result[i]; } std::cout << std::endl; } int main() { int num = 50; // Calculate factorial of 50 factorial(num); return 0; }
Output:
csharpCopy code
Factorial of 50 is: 30414093201713378043612608166064768844377641568960512000000000000
In this code, the
multiply
function is used to multiply the current result (stored in theresult
vector) with the numberx
. Thefactorial
function calculates the factorial of the given numbern
using themultiply
function iteratively. The result is stored in theresult
vector, which holds individual digits of the factorial.Note: Calculating the factorial of very large numbers can lead to very large results, and it's essential to use appropriate data structures and techniques to handle such cases.
Comments: 2
SCIAKU Team please upload 1st video of TREE please please please, please
I bought this course, it worth it!
Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it
Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website