Where is the mistake in my code to calculate factorials of numbers up to 99?
<p>I am using the old school multiplication method to calculate the factorials but still I am getting wrong answer for numbers greater than 13. I know that no data type can hold such a big value accurately so I am using an array to store each digit in the number separately.</p>

<p>"cas" in code denotes the number of which factorial is to be calculated.</p>

<p>Here is the code-</p>

<pre><code>void factorial (int cas)
{
  int num[158];
  num[0] = 1;

  for (int i=1; i&lt;158; i++)
  {
    num[i] = 0;
  }

  int row1[158];
  int row2[158];
  int length = 0;
  int max = 0;
  int carry = 0;

  for (int j=1; j&lt;=cas; j++)
  {
    for (int i=0; i&lt;158; i++)
    {
      row1[i] = 0;
      row2[i] = 0;
    }

    for (length=157; length&gt;=0; length--)
    {
      if (num[length] != 0)
      break;
    }

    if (j/10 &lt; 1)
    {
      int* ptr = num;
      carry = 0;

      for (int i=0; i&lt;=length; i++)
      {
        int mult = (j * num[i]) + carry;
        int units = mult % 10;
        carry = mult / 10;
        *ptr = units;
        ptr++;
      }
      *ptr = carry;
    }
    else
    {
      int* ptr = num;
      int* ptr1 = row1;
      row2[0] = 0;
      int* ptr2 = row2 + 1;
      carry = 0;

      for (int i=0; i&lt;=length; i++)
      {
        int mult = ((j%10) * num[i]) + carry;
        int units = mult % 10;
        carry = mult / 10;
        *ptr1 = units;
        ptr1++;
      }
      *ptr1 = carry;

      for (int i=0; i&lt;=length; i++)
      {
        int mult = ((j/10) * num[i]) + carry;
        int units = mult % 10;
        carry = mult / 10;
        *ptr2 = units;
        ptr2++;
      }
      *ptr2 = carry;

      for (max=157; max&gt;=0; max--)
      {
        if (row1[max] != 0 || row2[max] != 0)
        break;
      }

      carry = 0;

      for (int i=0; i&lt;=max; i++)
      {
        int add = row1[i] + row2[i] + carry;
        int units = add % 10;
        carry = add / 10;
        *ptr = units;
        ptr++;
      }
      *ptr = carry;
    }

  }
  for (length=157; length&gt;=0; length--)
    {
      if (num[length] != 0)
      break;
    }

  for (;length&gt;=0;length--)
  {
    printf("%i",num[length]);
  }

  printf("\n");
}
</code></pre>