Fibonacci sequence in c++

This is a discussion on Fibonacci sequence in c++ within the C++ Programming forums, part of the General Programming Boards category; im trying to write a program that will ask the user for "how many numbers in the Fibonacci sequence they ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    Fibonacci sequence in c++

    im trying to write a program that will ask the user for "how many numbers in the Fibonacci sequence they would like to see?".

    i need to also check to make sure that the number is at least two numbers the user wants to see. (using the if statement)
    and output the numbers in sequence (use a for loop)

    can someone create a program that will do this?
    i been trying to write this, but it is to difficult and i keep getting lost.

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      //How many numbers would you like to see? use cout
      //get number use cin
      //check that number >= 2 use an if statement
      //display numbers with a newline between each.  Probably using a loop.  use a for loop with a nested cout and an endl at the end
      //Use a wait command so the program doesn't exit
      return 0;
    }
    Turn my comments into code and you have your program.
    Last edited by ~Kyo~; 02-06-2011 at 06:58 PM. Reason: change and to an

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    i need some more help.im getting lost. im a noob at this.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Give us what you have saying code my homework for me won't get you far here at all.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    i got this far. i dont know how to form the if statement and for loop yet.
    \
    #include <iostream>
    using namespace std;

    int main (int argc, char* argv[])
    {
    cout<<"How many numbers would you like to see";
    getline(cin, input);

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    i got this far. i dont know how to form the if statement and for loop yet.
    \
    #include <iostream>
    using namespace std;

    int main (int argc, char* argv[])
    {
    cout<<"How many numbers would you like to see";
    cin.getline;

  7. #7
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    input is undefined at this point. You might even want a do... while loop instead on getting user input... Is it supposed to exit when they enter a number less than 2? Or re-ask the question?

    Code:
    #include <iostream>
    using namespace std;
    
    int main (int argc, char* argv[])
    {
       
       cout<<"How many numbers would you like to see";
       getline(cin, input); 
       
    
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    it is suppose to re-ask the question if it is lower than 2

  9. #9
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    define input as an int in ain before you use it.

    like: int x but using input instead.

    use a do.. while loop for the user getting info.

    so
    Code:
    do
    {
       //ask for input and get it
    }while(/*condition*/);  /*condition will use input and probably the number 2 you can use the following
     >,<,>=,<=,== so a > b if a is bigger than b; or a < b if a is smaller than b; a >= b a is bigger than or
     equal to b; a == b a is the same as b*/
    Last edited by ~Kyo~; 02-06-2011 at 07:35 PM. Reason: Word wrap fix

  10. #10

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    i keep on getting lost. ima need some help. i try putting in integers. i dont how to put the fibonacci sequence in code.

    #include <iostream>
    using namespace std;

    int main (int argc, char* argv[])
    {
    int fib1 = 0;
    int fib2 = 1;
    int fib3;
    int numbers;


    cout<<"How many numbers would you like to see";
    cin >> numbers;

    for (numbers >= 2;

    fib3 = fib1 + fib2;
    cout << fib3;
    fib1 = fib2;
    fib2 = fib3;
    system("pause");
    return 0;
    }

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    OK, let's just assume you knew how to calculate fibonacci numbers by hand. How many numbers would you have to calculate before you get to the 6th term? How about the 12,047th? You have to count n terms starting from m. So would the computer.

  13. #13
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Well the math for the sequence is really something like

    int fib1 = 0;
    int fib2 = 1;

    You could do something like

    fib2 = fib2 + fib1;
    fib1 = fib2 - fib1;

    every loop you would have 2 new numbers and fib2 will always be the new number...

    Code:
    #include <iostream>
    using namespace std;
    
    int main (int argc, char* argv[])
    {
       int fib1 = 0;
       int fib2 = 1;
       int fib3;
       int numbers;
       do
       {       
        cout<<"How many numbers would you like to see?";
        cin >> numbers;
        }while(numbers <= 2);
        cout<<fib1<<endl<<fib2<<endl;  //keep in mind you want to see the first 2 as well
        for(int loop = 2;loop < numbers;loop++)    
        {
            fib3 = fib1 + fib2;
            cout << fib3;
            fib1 = fib2;
            fib2 = fib3;
        }
        system("pause");
        return 0;
    }

  14. #14
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    A little bit on loops...

    for loops
    Code:
    for(
    /*some control could be defined outside the loop*/;
    /*some condition*/ ;
    /*something to modify the control*/)
    {
      //stuff to do
    }

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    this is how far i got. need more help

    #include <iostream>

    using namespace std;

    int main()
    {
    int fib;
    int fib1 = 0;
    int fib2 = 1;
    int n = 0;
    cout<<"How many numbers would you like to see? ";
    cin>> fib;
    cin.ignore();
    if ( fib < 2 );
    cout<<"please pick a number higher then 2\n";


    return (1);
    }

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n-th element of the fibonacci sequence
    By me001 in forum C Programming
    Replies: 22
    Last Post: 09-24-2008, 04:27 AM
  2. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 08:55 AM
  3. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  4. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 11:04 PM
  5. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 11:40 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21