Project Euler: Problem #2 solution in C++

/**(From:http://projecteuler.net/index.php?section=problems&id=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.

Solution:*/

#include <iostream>

using namespace std;

int main()
{

int term_1 = 1;
int term_2 = 2;
int term_3 = 0;
int MaxTerm = 0;
int sum = 0;

cout<< “This Program lists the Fibonacci sequence. Please enter the Max number you want to have sequence:” << endl;

cin >> MaxTerm ;

while (term_1 <= MaxTerm)
{
cout<< term_1 << endl;
if ((term_1 %2) == 0){sum += term_1;}

term_3 = term_1 + term_2;
term_1 = term_2;
term_2 = term_3;

}
cout << “the sum of even value numbers is: “<< sum << endl;
}

Advertisements

About Khuram Ali

Programming... Programming and Programming...!!!

Posted on March 25, 2013, in C++, Programming, Project Euler and tagged , . Bookmark the permalink. 2 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: