Fibonacci and prime numbers help!

Hi so my assignment is to write a program in which you input a number and you get A) all fibonacci numbers up to input & B) display fibonacci that are also prime. I have the fibonacci code like this.
1
2
3
4
5
6
7
8
9
10
11
12
int number(0), choice, A(0), B(1), total(0);
bool prime;
cout<<"Enter a number between 1 and 100:";
cin>>number;
cout<<endl;

{for(int i = 0; i <= number; i++){
			cout << total << endl; 
			total = A + B; 
			A = B;
			B = total;}
	}


but for some reason it does not stop ant the "number" keeps going and part B I have no Idea on how to go by it. Please help me anything would be appreciated!!!
Last edited on
That's because you're not getting all the elements of the Fibonacci series <=number. You're getting the first number elements of the series.
I dont know how to fix it, even if i make it =number I get the same response
What's with your syntax? The cout>>endl; statement shouldnt even work, because >> doesnt work with cout. And why do you have the entire for loop in it's own scope?
Last edited on
So you want the fibonacci numbers up to the user input as in:

User input = 14

Output:
0, 1, 1, 2, 3, 5, 8, 13

Correct?

If that is the case then why are you iterating from i =0 to i<=14? That will give you fifteen outputs. You probably want an if statement that compares total and number and stops when total is >= number and then returns everything but the last addition.

actually the way you have the code setup you could probably just do if(total<number) because total iterates through the code and is already the next fibonacci number.

So when you have number = 14, the fibonacci numbers will iterate out and after 13 comes out as total then total becomes 21 and will compare with number and then will have stopped due to the if statement no longer being satisfied.

sorry about the part with the cout and cin I added that while typing it on the page. the reason is because the for loop is within an if statement and the inputs are way before any of the code Im interested on. Lousy mistake.
You don't need to apologize, normally people are a bit more respectful than jp01cf01 was. This is a beginners forum for questions and as beginners we tend to make mistakes like that. Have you tried the if statement?
Sorry, I didnt realize I was being disrespecful xD I'm actually a beginner too, so I really didnt mean to sound condescending :P
Haha no worries. It sounded a little harsh but this is the internet so there is no reason to worry about what others think of personal work, all questions are good questions if you learn something. I bet that mistake doesn't happen again because cogirl will remember this thread!
@cogirl also you have some syntax errors you should take into account. You don't want the '{' in front of your for statement and you need to have a function definition and parameter list of some sort. ie:
1
2
3
4
5
6
7
8
9
10
11
int main(){

int ...
bool ..
cout..
cin...
cout..

// now your code to obtain the fibonacci sequence

}


Other options would be to create a function like get_fibonacci and have it setup as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void get_fibonacci( int number){

//code that couts your fibonacci sequence so long as total<number.

}

int main(){
int A,B,...
cout..
cin<<number;
cout<<endl;

get_fibonacci(number);
}

Last edited on
Im confused on the whole get_fibonacci I was trying using the if statements but it makes it way more complicated. Syntax, yes sorry again I just typed what thought was important to solve my problem >.<
Here is how I would do the first part of your question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void get_fibonacci(int number){
	int A=0, B=1, total=0;
	while(total<number){
		cout << total << endl; 
			total = A + B; 
			A=B;
			B=total;
	}
}

int main(){
int number;
cout<<"Enter a number between 1 and 100: ";
cin>>number;
cout<<endl;

get_fibonacci(number);

}
Last edited on
OMG..Omg Omg!!!! Thank you so freaking much!!! that was perfect!!!!
May I say I love you!
Topic archived. No new replies allowed.