### stuck on my sec program

hey fellas im im having this error "expected declaration before "}" token" on line 33
im using code:blocks and when i try to fix that i return nothing on the output

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182`` `````` #include #include using namespace std; int primeD (int b){ for (int i = 2; i < b; i++) { if (b % i == 0){ return i; } } } bool prime (int a) { for (int i = 2; i < a; i++) { if (a % i == 0) { return false; else return true; } } } int fibonacci (int f) { int c; int fib [1000]; fib [0] = 0; fib [1] = 1; for (int i = 2; i < 1000; i++) { fib[i] = fib[i-1] + fib[i-2]; if(fib[i] > f) { c = fib[i]; loop: fib[i] = fib[i-1] + fib[i-2]; c = fib[i]; while(!prime(c)) { goto loop; } return c; } } } int main() { int a = 227000; while(prime(fibonacci(a))) { a = (fibonacci (a)); } cout << a; }``````
Last edited on
closed account (jwkNwA7f)
 ``23242526272829303132`` ``````for (int i = 2; i < a; i++) { if (a % i == 0) { return false; else return true; } }``````

The else statement is inside the if statement. It should be:
 ``23242526272829`` ``````for (int i = 2; i < a; i++) { if (a % i == 0) return false; else return true; }``````
Last edited on
Why my program dont Print out the next prime number
Of 227300 and instead return 227300
Last edited on
??
Because the logic in the for loop is incorrect.

 ``1234567`` ``````for (int i = 2; i < a; i++) { if (a % i == 0) return false; else return true; }``````

So say I passed 9 into the function the first thing that happens is the if will have
 `if(9 % 2 == 0)`
which is false, so it will execute the else and return true. 9 is not prime, so this function should not be returning true with 9 as an input. I have not looked at your fibonacci function yet. Hopefully there is nothing wrong with the fibonacci function.
Topic archived. No new replies allowed.