Home > Mobile >  Program to find prime numbers from the set of numbers of Fibonacci series in C
Program to find prime numbers from the set of numbers of Fibonacci series in C

Time:11-09

I want to find prime numbers from the Fibonacci series after printing them. First, I implemented the code for Fibonacci then added each element into an array. Then passed the array to a method to check for prime. Wanted to try it with an array. Displaying the series but not the prime numbers from the following code.


#include <stdio.h>
int fib()
{
    int a=0,b=1, arr[20];
    arr[0] = a;
    arr[1] = b;
    printf("%d, %d,",a, b );
    int c=0;
    for(int i=2; i<=20; i  )
    {
        c=a b;
        arr[i] = c;
        printf("%d,",c);
        a=b;
        b=c;
    }
    checkPrime(arr);
}
void checkPrime(int a[])
{
    int i, count;
    for(i=0; i<sizeof(a); i  )
    {
        count=0;
        for(int j=2; j<=a[i]/2 ; j  )
        {
            if(a[i]%2==0)
                count  ;
        }
        if(count>1)
           printf("%d is a Prime", a[i]); 
    }
    
}
int main() 
{
    fib();
}

Output of the code

0, 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765, 8 is a Prime

CodePudding user response:

You have certain bugs in your implementation.

  1. In fib function you iterate until i <= 20 but your arr have length 20 therefore your loop will go out of bounds of array. You should iterate until i < 20 or increase length of your array.
  2. In checkPrime function you iterate until i < sizeof(a). But sizeof function doesn't return size of your array. It returns size of type of variable that you pass to it therefore it will return sizeof(int*) which = 8 (in case you 64 bit machine but I guess you have). To fix this bug you should pass length of your array in checkPrime function and use it.
  3. You don't reset the count variable after j loop.
  4. checkPrime function doesn't check if the number is prime. You have wrong expression in your nested loop. To check if number N is prime you should check if there any divisor of N that at least less than sqrt(N). Your expression is wrong.

Considering the adjustments above I suggest the next solution:

void checkPrime(int a[], size_t a_len) {
    int i, count;
    for (i = 1; i < a_len; i  ) {
        count = 0;
        for (int j = 2; j <= sqrt((double)a[i]); j  ) {
            if (a[i] % j == 0) {
                count  ;
                break;
            }
        }

        if (count == 0) {
            printf("%d is a Prime\n", a[i]);
        } else {
            count = 0;
        }
    }
}

int fib() {
    size_t arr_size = 21;
    int a = 0, b = 1, arr[arr_size];
    arr[0] = a;
    arr[1] = b;
    printf("%d, %d, ", a, b);
    int c = 0;
    for (int i = 2; i < arr_size; i  ) {
        c = a   b;
        arr[i] = c;
        if (i == arr_size - 1)
            printf("%d ", c);
        else
            printf("%d, ", c);
        a = b;
        b = c;
    }
    printf("\n");
    checkPrime(arr, arr_size);
}
  • Related