## Posted By

mju4t on 02/03/11

# Project Euler Problem 2

/ Published in: C++

Sorted via problem ID.

`/*Alexander DeTrano2/1/2010Project Euler - Problem 2 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.*/ #include <iostream>#include <vector> using namespace std; int fib(int n); int main() { vector<int> myArray; //declare vector to store Fibonacci numbers int s=34;  // Size needed for vector lengthint sum=0; //Initialize sum to 0 //Generate Fibonacci numbers and store in array, add every thirdfor(int i=0; i<s; i++){        myArray.push_back(i);        if (i%3 == 0){        myArray[i]=fib(i);        cout<< "fib(" << i << ")= " << myArray[i] << " \n";        sum+=myArray[i];cout<<"    Sum = "<< sum <<"\n";        }    } cout<<"Total sum = " << sum;} // Function to generate Fibonacci numbersint fib(int n){	if ( n==0 ) return 0;	if ( n==1 ) return 1; 	/*for(int i=0;i<5;i++){	z= fib(n-1) + fib(n-2);	*/	return fib(n-1) + fib(n-2);}`