Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is Sum of Even Terms In Fibonacci (<4million)? [Large Value Datatype Confusion]
    primarykey
    data
    text
    <p>By starting with 1 and 2, the first 10 terms of Fibonacci Series will be: </p> <p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</p> <p>Find the sum of all the even-valued terms in the sequence which do not exceed 4 million.</p> <hr> <p>Now, I got the idea for how to do this. But I'm confused about the data types to hold such big data. I'm getting weird results with <code>int</code>. :(</p> <p>MORE: Its Project Euler 2nd question. But I can't get it. I get crazy values as answer. Can someone please post the ideal program?</p> <p>EDIT: Here's what I wrote for just printing Fibonacci to screen. Bare Basic. My variable goes crazy even when I give 100 for the limit. Is my code wrong?</p> <pre><code>// Simple Program to print Fibonacci series in Console #include &lt;stdio.h&gt; int main() { int x=1,y=2,sum=0,limit=0,i=0,temp=0; printf("Enter Limit:"); scanf("%d",&amp;limit); if(limit==1) printf("%d",x); else if(limit&gt;1) { printf("%d %d",x,y); if (limit&gt;2) { while (i&lt;limit-2) { temp=y; sum=x+y; x=temp; y=sum; printf(" %d",sum); i++; } } } printf("\n"); return 0; } </code></pre> <hr> <p><strong>SOLVED: Actually, I managed to get the solution myself. Here's my program. It works.</strong></p> <pre><code>#include &lt;stdio.h&gt; int main() { int x=1,y=2,sum,limit; //Here value of first 2 terms have been initialized as 1 and 2 int evensum=2; //Since in calculation, we omit 2 which is an even number printf("Enter Limit: "); //Enter limit as 4000000 (4million) to get desired result scanf("%d",&amp;limit); while( (x+y)&lt;limit ) { sum=x+y; x=y; y=sum; if (sum%2==0) evensum+=sum; } printf("%d \n",evensum); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload