largest number in fibonacci series

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 5 Exercise 11
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 5 Exercise 11

#include "std_lib_facilities.h"

int main()
try
{
	vector<int> fibonacciNum{ 0,1 };
	bool fibBool = true;
	int counter = 0;

	while(fibBool){
		int nextFibNum = fibonacciNum[counter] + fibonacciNum[counter + 1];
		
		if (nextFibNum >= 0) {
			fibonacciNum.push_back(nextFibNum);
		}
		else {
			fibBool = false;
		}
		counter++;
	}

	for (int i = 0; i < fibonacciNum.size(); i++) {
		cout << fibonacciNum[i] << "\n";
	}

	cout << fibonacciNum.size() << " fibonacci integers fit into a int vector\n";
	cout << "The largest number in the int vector is: " << fibonacciNum[fibonacciNum.size() - 1] << "\n";

	keep_window_open(".");
}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
47 fibonacci integers fit into a int vector
The largest number in the int vector is: 1836311903

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.