Error in program to detect if two given numbers are consecutive fibonacci numbers

• 02-04-2012
abhishekcoder
Error in program to detect if two given numbers are consecutive fibonacci numbers
I have written this program to detect whether two numbers are consecutive terms of the fibonacci series. The code that I have written below gives me wrong answer when I enter 21 and 34 and 55 and 89 (gives right answer for other entries). Below is the fibonacci series representation:

1 1 2 3 5 8 13 21 34 55 89...

A fibonacci series is made up of consecutively adding two previous terms. Below is my code;

Code:

```#include<stdio.h> int main() {     int d,e,a,b,i;     printf("enter the numbers >1 to be checked");     scanf("%d%d",&d,&e);     while(a!=1 && (b!=1||0))     {         a=e-d;                                  b=d-a;         e=a;         d=b;     }     if(a==1 && (b==1||0))     {             printf("the given numbers were fibonacci series numbers");     }     else     {             printf("the given numbers were not fibonacci numbers");     }     system("pause");     return 0; }```
• 02-04-2012
oogabooga
Why do you have the user enter two numbers???
This:
Code:

`    while(a!=1 && (b!=1||0))`
should be this
Code:

`    while(a!=1 && b!=1 && b!=0)`
(presumably).
• 02-04-2012
abhishekcoder
I want the user to enter any number from the fibonacci series to check whether they are the consecutive terms. Also, the above correction doesn't make sense in the program as b cannot be simultaneously 0 and 1 at the same time - not logically correct. Thank you for your reply.

Quote:

Originally Posted by oogabooga
Why do you have the user enter two numbers???
This:
Code:

`    while(a!=1 && (b!=1||0))`
should be this
Code:

`    while(a!=1 && b!=1 && b!=0)`
(presumably).

• 02-04-2012
iMalc
Quote:

Originally Posted by abhishekcoder
Also, the above correction doesn't make sense in the program as b cannot be simultaneously 0 and 1 at the same time - not logically correct. Thank you for your reply.

Sorry, you are mistaken. oogabooga's correction is logically correct, and is in fact the fix for your bug.
An example of value that is simultaneously not one and not zero at the same time, is two.

What you wrote does not do what you think it does. The compiler starts by evaluating 1||0, which results in true. It then checkes if b is not equal to true, which through the wonders of type promotion results in comparing b against 1 (since true becomes 1).
• 02-08-2012
abhishekcoder
@oogabooga

Thank you for helping me out. I tried your code but it gives erroneous result for the consecutive inputs 21 and 34. Kindly help me out to fix this bug.
• 02-08-2012
sparkomemphis
Quote:

Originally Posted by abhishekcoder
@oogabooga

Thank you for helping me out. I tried your code but it gives erroneous result for the consecutive inputs 21 and 34. Kindly help me out to fix this bug.

• 02-08-2012
abhishekcoder
The current version of my programme is:

Code:

```#include<stdio.h> int main() {     int d,e,a,b,i;     printf("enter the numbers >1 to be checked");     scanf("%d%d",&d,&e);     while(a!=1 && b!=1 && b!=0)     {         a=e-d;                                b=d-a;         e=a;         d=b;     }     if(a==1 && (b==1||0))     {             printf("the given numbers were fibonacci series numbers");     }     else     {             printf("the given numbers were not fibonacci numbers");     }     system("pause");     return 0; }```
• 02-08-2012
jlewand
The if statement has some redunancy. while a != 1 AND..... means post-while loop, the if ( a == 1 ) part must be true 100% of the time so you could just leave the a check off?

A and B haven't been initialized. Do they default to 0? Seems so. The while loop doesn't get entered.
• 02-08-2012
oogabooga
To hopefully close this thread, here's a version of your program that works. Note that a and b need to be initialized to something not 0 or 1 (otherwise they are, in essence, randomly initialized, which may "work" but is still an error).
Code:

```#include<stdio.h> int main() {     int d,e,a=99,b=99;     printf("enter the numbers >1 to be checked");     scanf("%d%d",&d,&e);     while(a>1)     {         a=e-d;                                b=d-a;         e=a;         d=b;     }     if(a==1 && (b==1||b==0))     {             printf("the given numbers were fibonacci series numbers");     }     else     {             printf("the given numbers were not fibonacci numbers");     }     system("pause");     return 0; }```
• 02-08-2012
EuroDominus
You still have to fix line's 15 "if" statement as oogabooga has indicated above.
• 02-08-2012
abhishekcoder
Thank you oogabooga and EuroDominus! It worked!