Fibonacci using array

#include <stdio.h>
int main() {
int i, n,sum=0;
int fibo[n];
fibo[0]=1,fibo[1]=2;
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", fibo[0],fibo[1]);

for (i = 2; fibo[i] < n; ++i) {
    fibo[i]=fibo[i-1]+fibo[i-2];
}
for (i = 2; fibo[i] < n; ++i) {
       printf("%d, ",fibo[i]);
}
for (i = 0; fibo[i] < n; ++i) {
       if  (fibo[i]%2 == 0) {
           sum = 0;
            sum += fibo[i];
}
else {
    sum = sum;
}
}        printf("\n",sum);

return 0;

}

input
100
why am i getting output 2
instead of 44

    int i, n,sum=0;
    int fibo[n];

You haven’t given n a value at this point.

Edit:

That’s probably not the only thing wrong, but fix that first.

And please format your code next time :slight_smile:

1 Like