C language to achieve N factorial program code

  • 2020-04-01 23:46:32
  • OfStack

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#define N 10 //Take N factorial
int main()
{       //Array & have spent & have spent 1 1!
    int ary[N] = {1, 1};
    int i, j;
    for (i = 2; i <= N; i++)
    {
        //Factorial of the indices, the 0th index is the digit, so we start at the first place
        for (j = 1; j <= ary[0]; j++)
        {
            ary[j] = ary[j] * i;
        }
        //If we do carry, we're going to go into one place
        for (j = 1; j <= ary[0]; j++)
        {
            if (ary[j] >= 10000)
            {
                //carry
                ary[j+1] = ary[j+1] + ary[j] / 10000; 
                //carry And then you're left with the remainder 
                ary[j] = ary[j] % 10000;
            }
        }
        // There are carry So the number of digits is just +1
        //J is already bigger than 1
        if (ary[j] >= 1)
        {
            ary[0]++;
        }
    }

    //Reverse output
    for (j = ary[0]; j > 0; j--)
    {
        printf("%d", ary[j]);
    }
    printf("rn");
    return 0;
} 

Related articles: