Hi!

Programing a program with the fibonacci numbers, but having some problems getting the program to work as it should. The problem is that the user which is using the program are going to type a "max value" of how far he wants to count the fibonacci numbers.

So if the user sets the max value to 30. The program will write out 21, since that's the only number under 30.

Anyone?

Thnx:)

Very easy to do, heres an example:

#include<iostream>
using namespace std;

int main() {
	cout << "Enter maximum value: ";
	int max;
	cin >> max;
	//
	int sum = 0;  // old1 + old2
	int old1 = 0; // old
	int old2 = 1; // oldest
	//
	while (sum <= max) {
		cout << sum << '\n';
		sum = old1 + old2;
		old2 = old1;
		old1 = sum;
	}
	//
	cin.ignore();
	cin.ignore();
	return 0;
}

Sweeet;D Even easier than I thought, my program began to be more and more advansed:S But thnx allot for the help, mutch appreciated:)

Now i'm trying to get the numbers into a table, and then the user can say "I want number 5 in the table," and the program will write out the number which is sorted as nr. 5 in the table.

Can't get the numbers into the table:S

Just add the values to a vector instead of displaying them.

#include<iostream>
#include<vector>
using namespace std;

int main() {
	cout << "Enter maximum value: ";
	int max;
	cin >> max;
	//
	int sum = 0;  // old1 + old2
	int old1 = 0; // old
	int old2 = 1; // oldest
	//
	vector<int> values;
	//
	while (sum <= max) {
		values.push_back(sum);
		sum = old1 + old2;
		old2 = old1;
		old1 = sum;
	}
	//
	cout << "Enter index: ";
	int index;
	cin >> index;
	cout << "\nResult: " << values[index];
	//
	cin.ignore();
	cin.ignore();
	return 0;
}

If you just want to get the fibonacci depending on the index, you dont need to store all the values inbetween, so something like this will do:

#include<iostream>
using namespace std;

int main() {
	//
	cout << "Enter index: ";
	unsigned int index;
	cin >> index;
	//
	unsigned int sum = 0;
	unsigned int old1 = 0;
	unsigned int old2 = 1;
	//
	for (unsigned int i = 0; i < index; i++) {
		sum = old1 + old2;
		old2 = old1;
		old1 = sum;
	}
	//
	cout << "\nResult: " << sum;
	//
	cin.ignore();
	cin.ignore();
	return 0;
}

Nice nice nice, but now the problem is fairly strange. Now the max value isn't there, if I choose 70 as the max value, and index = 70 the number is way to high. Seems like when we'r putting the numbers into a table, the max value is gone, and the table keeps counting fabonacci numbers into space, insted of setting the max value range to "70."

For the last code I gave you, you should imagine it like this:

index	fibonacci value
0:	0
1:	1
2:	1
3:	2
4:	3
5:	5

So if you enter the index 3, the output will be 2.

hmm, i guess it's the same thing as above, but i'm just allergic to iostream..

#include <cstdio>
int main(void)
{
    int num, f = 1, s = 1, n;
    scanf("%d", &num);
    
    while(s <= num)
            {
                 n = f+s;
                 f = s;
                 s = n;
             }
        printf("%d", f);
    scanf("\n");
    return 0;    
}

does it work?
i don't get the table thing, do you want to put all the numbers of lower value than max in there?

This article has been dead for over six months. Start a new discussion instead.