0

I'm basically done with this, the only problem is when it loops to check for the prime number, it prints out prime numbers that are not in the Fibonacci sequence as well. Here is my code:

int main()
{
  int no1,no2,newno,pno,i,terms,j;
  no1 = 0;
  no2 = 1;


  printf("**Fibonacci and Prime Numbers**\n\n");
  printf("Enter number of terms: \n");
  scanf("%d", &terms);

  printf("\nAmong the first %d terms of Fibonacci series that are also prime number: \n", terms);

  for(i=0; i<terms; i++){
    if(i<=no2){
        newno = i;
    }
    else{
        newno = no1+no2;
        no1 = no2;
        no2 = newno;
    }
  }

  for(pno=2;pno<=newno;pno++){
    for(j=2;j<=pno;j++){
        if(pno%j==0){
            break;
        }
    }
    if(pno==j){
        printf("%d \n", pno);
    }
  }


  getch();
  return 0;
}

I'm guessing its because of the pno++ , Am i right?

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
Ned
  • 23
  • 1
  • 7

3 Answers3

0

Try this

for(i=0; i<terms; i++){

 newno = no1+no2;
 no1 = no2;
 no2 = newno;

 for(j=2;j<=newno;j++){
    if(newno%j==0){
        break;
    }
 }
 if(newno==j){
    printf("%d prime\n", newno);
 }
}
omurbek
  • 742
  • 1
  • 7
  • 23
  • I tried it and it doesn't work, now it either prints one number or no number at all. It only prints the the number exactly before our input (if the number is a prime and Fibonacci) – Ned Oct 10 '17 at 05:29
0

This is because you are checking only last number. You should check every number you generate. Consider creating function e.g. is_prime and check every fibonnaci number you compute. Result could look like this (taken from C - how to test easily if it is prime-number?)

int is_prime(int num)
{
    if (num <= 1) return 0;
    if (num % 2 == 0 && num > 2) return 0;
    for(int i = 3; i < num / 2; i+= 2)
    {
        if (num % i == 0)
            return 0;
    }
    return 1;
}

And your main loop that calls is_prime for every number:

for (i = 0; i<terms; i++) {
    if (i <= no2) {
        newno = i;
    }
    else {
        newno = no1 + no2;
        no1 = no2;
        no2 = newno;

        if (is_prime(newno)) {
            printf("%d\n", newno);
        }
    }
}
vasek
  • 2,759
  • 1
  • 26
  • 30
  • Is it possible to do this without using a function? Cause I did one where I use function and it works. So I'm wondering if it is possible to do so without using a function. – Ned Oct 10 '17 at 06:33
  • Sure that it is possible, but in real-life programming, you would solve it using a function. – vasek Oct 10 '17 at 06:42
0

This is just a rough try of what you are trying to achieve .

 #include<stdio.h>

int main()
{
int no1,no2,newno,pno,i,terms,j;
no1 = 0;
no2 = 1;


printf("**Fibonacci and Prime Numbers**\n\n");
printf("Enter number of terms: \n");
scanf("%d", &terms);

printf("\nAmong the first %d terms of Fibonacci series that are also prime number: \n", terms);

for(i=0; i<terms; i++)
{

        newno = no1+no2;
        no1 = no2;
        no2 = newno;

    for(pno = 2; pno <= newno ;pno++)
    {

        if(pno == newno)
        printf("%d\n",newno);
        if(newno%pno == 0)
        break;
    }

    }
return 0;
    }

You were only trying to check only the last term and that to in incorrect way. The for loop you used

for(pno=2;pno<=newno;pno++){
for(j=2;j<=pno;j++){
    if(pno%j==0){
        break;
    }
}
if(pno==j){
    printf("%d \n", pno);
}

}

was applying check on local counter pno that does not make any sense.

You can further optimize your program using a better way to find if a number is prime as it will take huge time for large inputs.

Jasmeet
  • 1,315
  • 11
  • 23