# Thread: For loop fibonacci and prime number getting program not working

1. ## For loop fibonacci and prime number getting program not working

I have two programs, a program that outputs the Fibonacci sequence with for loops, and a program that determines if a entered number is prime. The Fibonacci one prints the incorrect result, and the prime number one has incorrect output and the result seems to be based on the return statements. Here is my code:

For Fibonacci:
Code:
```#include <iostream>
using namespace std;
int main ( ) {
int count = 0;
cout << "This program lists the Fibonacci sequence." << endl;
cout << "How many terms? ";
cin >> count;
int tracker = 0;
cout << "F(0) = " << tracker << endl;
tracker = 1;
cout << "F(1) = " << tracker << endl;
for(int track = 2; track < count; track++) {
int output = tracker + track;
int ovar = track;
ovar = tracker;
output = ovar + tracker;
cout << "F(" << track;
cout << ") = " << output;
cout << "" << endl;

}
}```
for Prime numbers:
Code:
```#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track;
track++;
if(prime > track) {
if(prime / track - 1 == 0) {
return 1; // I don't know what to return, this is a placeholder
}
else {
return 0; // placeholder
}

}

else {
break;
}
}
}
int main ( ) {
int number;
cout << "Enter number: ";
cin >> number;
cout << "" << endl;
cout << isPrime(number);
if ( isPrime(number) ) {
cout << number << " is not prime.";
}
else {
cout << number << " is prime.";
return 0;
}

}```
Please help. Also, do NOT post complete code as a reply, only help. 2. Your code is clearly based on guesswork, rather than systematic approach. Throw it out. Write down on paper (i.e. without going near a keyboard) a precise step-by-step description of how you would address each problem (producing Fibonacci sequence, or testing if a value is prime). Once you have that description in a clear form (i.e. you could give it to someone else who knows nothing about the problems, and they could follow it) then work out how to translate that description into code.

If you are going to post code, make sure that it is sensibly formatted (indentation, etc). That doesn't matter for a compiler, but does matter to people who might try to help you. 3. Any other help? 4. How about you do what was asked of your in the previous post? If your code doesn't make sense, then we need to make sure you understand the problem. Throwing a bunch of random stuff together isn't going to help. Once you show that you have a clear understanding of the problem, we can help you turn it into code.
If you don't fully understand the problem, then that's fine too. We can help you get an understanding.
What we won't do is give you a solution. You need to show us that you are willing to put in the effort needed to solve this. 5. I don't fully understand the problem. 6. Then you need to elaborate on what you don't understand. Do you understand the algorithm itself? Do you understand what fibonacci and primes are? 7. Have you learn recursion yet? This would be a great problem for that!!!!

If you have not learned that yet get a piece of paper and write down what is happening in the loop or use breaks in your compiler.

If you do not fully understand the problem you are solving you will waste your time looking for an easy to find solution to jump out at you on google. Popular pages Recent additions 