Thread: hwhere to put the msg in Fabonacci series

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    hwhere to put the msg in Fabonacci series

    Hi friends

    I am having trouble putting the "not a fabonacci number" msg in my code

    , plz help


    /*
    Name: fabonacci series and searching of element
    Copyright: devesh
    Author:
    Date: 14/12/10 01:29
    Description:
    */
    14/12/10 01:29
    #include<iostream.h>
    #include<conio.h>

    int main()


    {
    int fab,x1=0,x2=1,no,a[18];

    for(int i=0;i<18;i++)
    {
    fab=x2+x1;
    x1=x2;
    x2=fab;
    a[i]=fab;
    cout<<fab<<" ";
    }


    cout<<"\n\n enter the no to test :";
    cin>>no;

    for(int j=0;j<18;j++)

    {
    if(a[j]==no )
    {
    cout<<"\n\n Fabonacci";
    break;
    }
    }
    getch();
    }

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by deva View Post
    Hi friends

    I am having trouble putting the "not a fabonacci number" msg in my code

    , plz help

    Code:
    /*
      Name: fabonacci series and searching of element
      Copyright: devesh
      Author: 
      Date: 14/12/10 01:29
      Description: 
    */
    14/12/10 01:29
    #include<iostream.h>
    #include<conio.h>
    
    int main()
    
    
    {
        int fab,x1=0,x2=1,no,a[18]; 
        
        for(int i=0;i<18;i++)
        {
        fab=x2+x1;
        x1=x2;
        x2=fab;
        a[i]=fab;
        cout<<fab<<"  ";
        }
        
        
        cout<<"\n\n enter the no to test :";
        cin>>no;
        
        for(int j=0;j<18;j++)
        
        {
        if(a[j]==no )
        {
        cout<<"\n\n Fabonacci";
        break;     
        }
        }
        getch();
    }
    you can't really do that in the for loop. How about checking for true or false in the for loop and then write the statement outside.

    Code:
    bool fib = false;
    for(int j=0;j<18;j++)
    {
      if(a[j]==no )
         {
           fib = true;
           break;     
        }
    }
    if ( fib == true)
       std::cout << no << " is a fib" << std::endl;
    else
      std::cout << no << " is not a fib << std::endl;
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Did you see this popup when you submitted your code?

    If you did, what did you do about it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM