Write a factorial program where n=10,000,000
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
public class Main {
public static void factorial(int n)
{
long res[] = new long[1000000000];
res[0] = 1;
int res_size = 1;
for (int x = 2; x <= n; x++)
res_size = multiply(x, res, res_size);
System.out.println("Factorial of given number is ");
for (int i = res_size - 1; i >= 0; i--)
System.out.print(res[i]);
}
public static int multiply(int x, long res[], int res_size)
{
long carry = 0;
for (int i = 0; i < res_size; i++)
{
long prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod/10;
}
while (carry!=0)
{
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
public static void main(String args[])
{
factorial(10000000);
}
}
Get Answers For Free
Most questions answered within 1 hours.