title: Pascal's Triangle in C
body: <p>I'm a computer engineering student and next semester I am going to start C course. So in order to prepare myself a bit, I have started learning C by myself and stumbled across an interesting task, designed for, how it seemed to me at first sight, not a very advanced level.</p>
  
  <p>The task is to write a program to compute the value of a given position in <strong>Pascal's Triangle</strong>. And the formula given to compute it is written as <strong>element = row! / ( position! * (row - position)! )</strong> </p>
  
  <p>I've written a simple console program that seems to work okay, until I get to testing it with <strong>large</strong> numbers.</p>
  
  <p>When trying this program with row 16 and position 3, it calculates the value as 0, although it's obvious that there can't be such a value (in fact it should compute the value as 560), all cells of this triangle are supposed to be integers and be greater than one.</p>
  
  <p>I suppose I'm experiencing a problem with <strong>storing and processing large numbers</strong>. The factorial function seems to work okay, and the formula I used works until I get to trying large numbers</p>
  
  <p>So far the best solution was found here - <a href="https://stackoverflow.com/questions/2844/how-do-you-printf-an-unsigned-long-long-int">How do you printf an unsigned long long int(the format specifier for unsigned long long int)?</a>  using inttypes.h library with type uint64_t but it still doesn't give me the result I need.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;inttypes.h&gt;
  
  void clear_input(void);
  uint64_t factorial(int x);
  
  int main()
  {
      // Printing
      printf("This program computes the value of a given position in Pascal's Triangle.\n");
      printf("You will be asked for row and position of the value.\n");
      printf("Note that the rows and positions starts from 0.\n");
      printf("\n");
      printf("     1          * 0 \n");
      printf("    1 1         * 1 \n");
      printf("   1 2 1        * 2 \n");
      printf("  1 3 3 1       * 3 \n");
      printf(" 1 4 6 4 1      * 4 \n");
      printf(" ****************   \n");
      printf(" 0 1 2 3 4          \n");
      printf("\n");
  
      // Initializing
      int row, pos;
  
      // Input Row
      printf("Enter the row: ");
      scanf("%d", &amp;row);
      clear_input();
  
      // Input Position
      printf("Enter the position in the row: ");
      scanf("%d", &amp;pos);
      clear_input();
  
      // Initializing
      uint64_t element, element_1, element_2, element_3, element_4;
  
      // Previously written as -&gt; element = ( factorial(row) ) / ( factorial(pos) * factorial(row - pos) );
      // Doesn't fix the problem
      element_1 = factorial(row);
      element_2 = factorial(pos);
      element_3 = factorial(row - pos);
      element_4 = element_2 * element_3;
  
      element = element_1 / element_4;
  
      // Print result
      printf("\n");
      printf("%"PRIu64"\n", element_1);   // Temporary output
      printf("%"PRIu64"\n", element_2);   // Temporary output
      printf("%"PRIu64"\n", element_3);   // Temporary output
      printf("%"PRIu64"\n", element_4);   // Temporary output
      printf("\n");
      printf("The element is %"PRIu64"", element);
      printf("\n");
  
      return 0;
  }
  
  void clear_input(void)                                          // Temporary function to clean input from the keyboard
  {
    while(getchar() != '\n');
  }
  
  uint64_t factorial(int x)                                       // Function to calculate factorial
  {
      int f = 1, i = x;
      if (x == 0) {
          return 1;
      }
      while (i != 1) {
          f = f * i;
          i = i - 1;
      }
      return f;
  }
  </code></pre>