Find the factorial of 1000 in the C language.

In C language, storing the factorial of 1000 directly in a variable is not possible due to its size. Instead, we can use an array to store large integers and perform multiplication operations. Below is the C code to calculate the factorial of 1000.

#include <stdio.h>

#define MAX_SIZE 10000

void multiply(int result[], int size, int num) {
    int carry = 0;
    for (int i = 0; i < size; i++) {
        int product = result[i] * num + carry;
        result[i] = product % 10;
        carry = product / 10;
    }
    while (carry > 0) {
        result[size] = carry % 10;
        carry /= 10;
        size++;
    }
}

void factorial(int n) {
    int result[MAX_SIZE] = {0};
    result[0] = 1;
    int size = 1;

    for (int i = 2; i <= n; i++) {
        multiply(result, size, i);
    }

    printf("%d! = ", n);
    for (int i = size - 1; i >= 0; i--) {
        printf("%d", result[i]);
    }
    printf("\n");
}

int main() {
    factorial(1000);
    return 0;
}

The output is:
The result of 1000 factorial is 402387260… (with a total of 2568 digits, some of which are omitted).

Leave a Reply 0

Your email address will not be published. Required fields are marked *