title: How to convert floating-point in for-loop to integer in C, for correct calculation
body: <p>I'm trying to understand C better. In an exercise I had to find out what is wrong about the following code example.</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  int main() {
      int count;
      float sum;
      float i;
  
      sum = 0, count = 0;
      for (i = 1000; i &lt;= 1000.04; i += .01) {
          sum += i;
          count++;
      }
      printf(&quot;Sum: %f, Count: %d\n&quot;, sum, count);
  
      return 0;
  }
  </code></pre>
  <p>I found out that it's a bad idea to use floating-point in loops because it causes problems bsince it's not accurate. Next step is to rewrite the code, so it does the same thing but without using floating-point in the loop. I'm stuck on this task, I don't know how to replace <code>i &lt;= 1000.04</code>. For <code>i += .01</code> I guess I could replace it with <code>i++</code> and divide it with 100 somewhere else.</p>
  <p>Any ideas how to fix it properly?</p>