In my C++ code If I print sum outside loop it gives correct answer, but not in any loop for/while, why not?
<pre><code>int main()
{
  long long int first=0,second=1,T,N;     //here T is Number of Cases

  cin&gt;&gt;T;
  long long int fab=first+second;
  long long int sum[T];

  for(long long int i=0;i&lt;T;i++)
  {
    cin&gt;&gt;N;

    while(fab&lt;N)
    {
      first=second;
      second=fab;
      if(fab%2==0)
      {
        sum[i]+=fab;
      }
      fab=first+second;
    }
  }

for(int i=0;i&lt;T;i++)
{
  cout&lt;&lt;sum[i]&lt;&lt;endl;
}
  return 0;
}
</code></pre>

<p>In the above for loop sum is not providing the correct answer but if sum is used outside for loop then it gives appropriate answer.</p>