我基本上完成了这个,唯一的问题是,当它循环检查素数时,它打印出不在斐波那契数列中的素数。这里是我的代码:检查Fibonacci总理的数量
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;
}
我猜它是因为pno++
,对吗?
我试了一下,它不起作用,现在它打印一个数字或根本没有数字。它只在我们的输入之前打印数字(如果数字是素数和斐波那契) – Ned