2
#include <iostream>
using namespace std;
int main() 
{
   int a=1;
   int b=0;
   int c=0;

   while(c<=100) 
   {
       cout << c << "\n";             
       b=a;
       a=c;
       c=a+b;              
   }

   system("pause");    
   return 0;    
}

this is what i got to show the fib series up to 100 but i was actually interested in learning a way to introduce a number and for the program to tell me if the number is in the series. Anyone can tell me the changes i can make to do that?

  • 5
    Why not give it a try yourself then when you run into a problem post a question about any specific issues you encounter? Much more productive than just saying "hey, gimme teh codez!" – Captain Obvlious Sep 11 '14 at 15:50
  • 1
    use [Binnet's Formula](http://en.wikipedia.org/wiki/Jacques_Philippe_Marie_Binet) and what amounts to a binary search to see which (if any) fibonnaci term your number is. – Marc B Sep 11 '14 at 15:51
  • you need one simple comparison in the loop.... BTW: the 100th fibonacci number won't fit in an integer. – Karoly Horvath Sep 11 '14 at 15:51
  • Check out this question http://stackoverflow.com/questions/2432669/test-if-a-number-is-fibonacci – grzkv Sep 11 '14 at 15:54
  • @MarcB: right, just what a beginner needs... /sarcasm. – Karoly Horvath Sep 11 '14 at 15:59

1 Answers1

0

You could pass it as a parameter into the main function and then in the while loop check if the new c is equal to the number you passed in. If it is, break the loop and output the success message.

If you wanted to check if it was in the Fibonacci sequence for numbers after 100, break the loop when c becomes bigger than the number passed in and display a failure message.