Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 95,475 C++ Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 967 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert

Register to Make This Box Go Away!



Print Fibonacci series using do-while

Print Fibonacci series using do-while

Submitted By: eXceed69
Actions:
Rating:
Views: 10,503

Language: C++

Last Modified: December 9, 2006

Snippet


  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.      //define first two fibonacci series numbers.
  6.     int fib1 = 0;
  7.     int fib2 = 1;
  8.  
  9.      //declare the variable to store the next number of fibonacci series
  10.     int fib3;
  11.    
  12.      //declare the variable to store how many numbers to be printed. Default is 2.
  13.      int numbers = 2;
  14.  
  15.      //the counter to keep track how many numbers are printed.
  16.      int counter = 2;
  17.      
  18.      //Ask user how many numbers of the fibonacci series need to be printed.
  19.      std::cout << "How many Fibonacci number you need ? : " ;
  20.      
  21.     //Store the number.
  22.     std::cin >> numbers;
  23.  
  24.      //If number entered is less than 3, exit the program.
  25.     if (numbers < 3) return 0;
  26.  
  27.      //Print the first two element.
  28.     std::cout << fib1 << "\t" << fib2;
  29.  
  30.      //do-while loop to calculate the new element of the series and printing the same.
  31.     do {
  32.         counter++;
  33.         fib3 = fib1 + fib2;
  34.         std::cout << "\t" << fib3;
  35.         fib1 = fib2;
  36.         fib2 = fib3;   
  37.     } while (counter <= numbers);
  38.      
  39.     std::cout << std::endl;
  40.     system("pause");
  41.      return 0;
  42. }
  43.  

Copy & Paste


Comments


mhero_04 2007-11-23 07:15:30

eow!! can u make a code for fibonacci series using for loop?? please..

nirvanarupali 2007-12-11 20:05:38

What happen if I enter 100?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->