samedi 1 juillet 2017

Even Fibonacci numbers

I am solving project Euler problems. and I am not able to solve this problem.

  • **Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.**

I try to execute my program but it is giving this answer 2461277582 instead of 4613732. My code is

#include<iostream> 
using namespace std;

 int main()
{
     int  n;
     unsigned long int x=1,y=0,z,sum=0;

     cout<<"Enter the Number of terms:";
     n=4000000;

    cout<<"Your fibonacci series"<<" "<<n<<" "<<"terms are:"<<"\n";

     for(int i=0; i<n;i++)
     {
      z=y+x;
      y=x;
      x=z;
      if(z%2==0)
        {sum =sum+z;}
     }
    cout<<"Sum is"<<sum;
 }

PLease help me.

Aucun commentaire:

Enregistrer un commentaire