Fibonacci sequence

Hello everyone,

I have an assignment that requires me to create a code which displays the sum of all even fibonacci numbers. For example, the user inputs 15, the program should display - sum = 10. Determining the sum of all even fibonacci numbers less than or equal to 15 (2 and 8). I am wondering how to make the program display the sum of the even numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include<iostream>
#include<string>
using namespace std;

int main() {
	//declarations
	int userNum = 0;
	int counter = 0;
	int firstNum = 0;
	int secondNum = 1;
	int thirdNum = 0;
	string output = "";
	bool isUserNumValid = false;

	//get user input
	while (isUserNumValid == false) {
		cout << "Enter a whole number representing the maximum possible Fibonacci term allowed: " << endl;
		cin >> userNum;
		if (userNum > 0)
			isUserNumValid = true;
		else
			isUserNumValid = false;
	}
	{
		//calculations
		counter = 0;
		while (counter < userNum) {
			output = output + to_string(firstNum) + " ";
			thirdNum = firstNum + secondNum;
			firstNum = secondNum;
			secondNum = thirdNum;
			counter++;
		}
		//output
		cout << "Sum = " << output << endl;
		system("pause");
		return 0;

	}
}
You have three issues here. You're treating the value entered by the user as if it is the number of terms to generate instead of the maximum value, you need to be able to detect if a value is even, and you need to actually sum the values (which you are not currently doing.)

Are you allowed to put your code in functions? Do you know how to detect if a value is even?
Thank you so much, being a beginner i'm very blind in code right now. I hadn't even thought to use functions. Sadly I don't know how to detect even values.
Well, let's take the generation of the fibonacci sequence out of main, so you can concentrate on the logic you need to concentrate on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<string>
using namespace std;

// Not a fan of global variables, and there are other (better) ways to do this,
// but this requires the least amount of explanation.  These two variables define
// state for our next_term function.
unsigned current_term = 0;
unsigned previous_term = 1;

unsigned next_term()
{
    unsigned result = current_term;

    unsigned next = current_term + previous_term;
    previous_term = current_term;
    current_term = next;

    return result;
}


int main() {
    //declarations
    int userNum = 0;
    bool isUserNumValid = false;

    //get user input
    while (isUserNumValid == false) {
        cout << "Enter a whole number representing the maximum possible Fibonacci term allowed: " << endl;
        cin >> userNum;
        if (userNum > 0)
            isUserNumValid = true;
        else
            isUserNumValid = false;
    }
    {
        unsigned term = next_term();
        unsigned evens_sum = 0;

        while (term <= userNum)
        {
            // if term is even, add it to evens_sum

            term = next_term();
        }

        cout << "Sum = " << evens_sum << endl;
    }
}


To detect if a value is even, look up the modulus operator.

Hi Cire,

I'm learning too, this is just a question.
Had he been using doubles, floats or whatever, I wouldn't have blinked at the modulud operation suggestion, However, he's using using integers, so do you need still Modulus operators? Since dividing an integer always rounds down, Couldn't you as easily use something like this?

1
2
3
4
5
if (((usernum/2)*2)== usernum){
cout << "We have an even number! " << endl;
else
{
cout << "That's an odd number!" << endl;


Couldn't you as easily use something like this?

Yes you can use a more complicated, less efficient method. There is almost always more than one way to do something when programming.
Break the problem into chunks.

First figure out how to compute the Fibonacci sequence up to the number you need it to. Once you have that figured out, use the for-loop and modulus (%) operator to access and sum up the even numbers in the sequence.

As cire said, you may want to split this program up into different functions.
Topic archived. No new replies allowed.