title: Binary to decimal using horner
body: <p>1 x 2<sup>0</sup> +1 x 2<sup>1</sup> + 1 x 2<sup>2</sup> + 0 x 2<sup>3</sup> +1 x 2<sup>4</sup> = 1 + 2 x (1 + 2 x (1 + 2 x (0 + 2 x 1) ) )</p>
  
  <p>Recall <code>b[0] = 1, b[1] = 1, b[2] = 1,b[3] = 0, b[4] = 1</code></p>
  
  <pre><code>/* to convert a binary representation to a decimal one*/
  
  int dec, b[5] = {1, 1, 1, 0, 1};
  dec = b[4];
  for (int i = 3; i &gt;= 0; i--)
  {
      dec=2 * dec + b[i];  //horner's scheme
  }
  cout &lt;&lt; dec &lt;&lt; endl;
  </code></pre>
  
  <p>I tried to write this code again in C language, but it's not working correctly:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int main()
  {
      int B[5];
      int x, s, s1;
  
      for(int i = 1;i &lt;= 5; i++)
      {
          printf("Enter %d. digit of binary number", i);
          scanf("%d", &amp;B[i]);
      }
  
  
      s = B[5];   /*this part for reverse the array*/
      B[5] = B[1];
      B[1] = s;
  
      s1 = B[4];
      B[4] = B[2];
      B[2] = s1; 
  
      x = B[4];
  
      for (int i = 3; i &gt;= 0; i--)
      {
          x = 2 * x + B[i];  
      }
  
      printf("%d", x);
      scanf("%d");
  }
  </code></pre>