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

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    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;
    }
    Thank you for your help.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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 View Post
    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).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by abhishekcoder View Post
    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).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    @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.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by abhishekcoder View Post
    @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.
    Please post your current version.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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;
    }

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    44
    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.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    You still have to fix line's 15 "if" statement as oogabooga has indicated above.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank you oogabooga and EuroDominus! It worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. Fibonacci numbers in C
    By cashmerelc in forum C Programming
    Replies: 2
    Last Post: 09-28-2007, 05:35 PM
  3. Fibonacci numbers
    By Cela in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2003, 09:26 AM
  4. FIBONACCI NUMBERS, please help!
    By JamesAnthony23 in forum C Programming
    Replies: 5
    Last Post: 09-26-2002, 03:39 PM
  5. fibonacci numbers?????
    By help!! in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 11:07 PM