title: Simplified Polynomials
body: <p>I'm trying to print out the Lagrange Interpolation Form.</p>
  <p><a href="https://i.stack.imgur.com/1JEqs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1JEqs.png" alt="enter image description here" /></a></p>
  <pre><code>    double x0 = x[0];
      double x1 = x[1];
      double x2 = x[2];
      double x3 = x[3];
      double z0 = z[0];
      double z1 = z[1];
      double z2 = z[2];
      double z3 = z[3];
  if (size == 2)
      {
          cout &lt;&lt; &quot;\nLagrange Polynomial Form: &quot; &lt;&lt; endl;
          cout &lt;&lt; (1 / (x0 - x1)) * (z0) &lt;&lt; &quot;(x&quot; &lt;&lt; -x1 &lt;&lt; &quot;)&quot; 
               &lt;&lt; (1 / (x1 - x0)) * (z1) &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot; &lt;&lt; endl;
  
      }
      else if (size == 3)
      {
          cout &lt;&lt; &quot;\nLagrange Polynomial Form: &quot; &lt;&lt; endl;
          cout &lt;&lt; (1 / ((x0 - x1) * (x0 - x2)) * (z0)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x1 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot;
               &lt;&lt; (1 / ((x1 - x0) * (x1 - x2)) * (z1)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot;
               &lt;&lt; (1 / ((x2 - x0) * (x2 - x1)) * (z2)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot;;
  
  
      }
      else if (size == 4)
      {
          cout &lt;&lt; &quot;\nLagrange Polynomial Form: &quot; &lt;&lt; endl;
          cout &lt;&lt; (1 / ((x0 - x1) * (x0 - x2) * (x0 - x3)) * (z0)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x1 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x3 &lt;&lt; &quot;)&quot;
              &lt;&lt; (1 / ((x1 - x0) * (x1 - x2) * (x1 - x3)) * (z1)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x3 &lt;&lt; &quot;)&quot;
              &lt;&lt; (1 / ((x2 - x0) * (x2 - x1) * (x2 - x3)) * (z2)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x1 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x3 &lt;&lt; &quot;)&quot;
              &lt;&lt; (1 / ((x3 - x0) * (x3 - x1) * (x3 - x2)) * (z3)) &lt;&lt; &quot;(x&quot; &lt;&lt; -x0 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x1 &lt;&lt; &quot;)&quot; &lt;&lt; &quot;(x&quot; &lt;&lt; -x2 &lt;&lt; &quot;)&quot;;
  
  
      }
  </code></pre>
  <p>What I am trying to do is read the size of the given array then print out the formula base on size = 1,2,3... But it took too much line of code if I keep doing until size = 10. How can I simplify my code to for loop or something that easier?</p>