title: Binomial Theorem - algorithm in C
body: <p>I'm trying to find a solution(fix errors) in my programme which must count the binomial theorem from definition. Firstly I created the definition of "<strong>factorial</strong>" - "silnia".</p>
  
  <p>1) The algorithm determines the value of SN1 (n,k) of the definition. (<strong>newton</strong> function)</p>
  
  <p>2) The algorithm determines the value of SN3  (n,k) recursively by the formula. (<strong>newton_rek</strong> function).</p>
  
  <p><strong>INPUT:</strong>
  File name: In0101.txt </p>
  
  <p><strong>OUTPUT:</strong>
  File name: Out0101.txt 
  In this file I want to save the values ​​calculated from the formulas.</p>
  
  <p><strong>EXAMPLE:</strong>
  In0101.txt</p>
  
  <pre><code>8 2// n k 
  </code></pre>
  
  <p>Out0101.txt</p>
  
  <pre><code>n=8 k=2
  SN1 = 28; count= 14
  </code></pre>
  
  <p>And there is an error I can't fix. Does anybody can help me with this ?</p>
  
  <p><strong>MY CODE:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  long silnia(int a)
  {
      long s;
      if (a == 0 || a == 1)
      {
          return 1;
      }
      else
      {
          s = 1;
          for (int i = 1; i &lt;= a; i++)
          {
              s *= i;
          }
      }
      return s;
  }
  
  long newton(int n, int k)
  {
      return silnia(n)/(silnia(k)*silnia(n-k));
  }
  
  unsigned long int newton_rek(long int n ,long int k)
  {
      if ( n == k || k == 0 )
      {
          return 1;
      }
  
      if (k &gt; n)
      {
          return 0;
      }
  
      else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
  }
  
  int main()
  {
      int n = 0;
      int k = 0;
      long funkcja1 = 0;
      long funkcja2 = 0;
  
      FILE *f = fopen("In0101.txt", "r+");    
      if (f == NULL)
      {
          printf("Nie udalo sie otworzyc pliku In0101.txt\n");
          return 1;
      }
      fread(n, sizeof(long), 1 , f);
      fread(k, sizeof(long), 1 , f);
      fclose(f);
  
      FILE *ff = fopen("Out0101.txt", "w+");    
  
      if (ff == NULL)
      {
          printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
          return 1;
      }
  
      funkcja1 = newton(n,k);
      funkcja2 = newton_rek(n,k);
      fwrite(funkcja1, sizeof(long), 1 , ff);
      fwrite(funkcja2, sizeof(long), 1 , ff);
      fclose(f);
  
      return 0;
  }
  </code></pre>