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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include <iostream>
#include <array>

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)
23
24
25
26
27
28
29
30
31
32
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:
23
24
25
26
27
28
29
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.

1
2
3
4
5
6
7
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.