Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,263 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,250 people online right now. Registration is fast and FREE... Join Now!





Print Fibonacci series using do-while

Print Fibonacci series using do-while

Submitted By: eXceed69
Actions:
Rating:
Views: 28,078

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?

JorisValencia 2008-09-01 06:58:35

why does this code have errors in my turbo c?

lorelie 2008-10-14 05:58:12

hi can you create a pr5ogram that generates N series of the fibonaccci nos. with MAX of 50 using an array,, please..

fa4han 2008-11-02 23:49:31

#include using namespace std; int main() { int fib1 = 0; int fib2 = 1; int fib3; cout

Migs 2008-11-24 14:06:03

Here is a simpler way and it deals with first two numbers:-Migs /* Given: n A non-negative integer. Task: To find the nth Fibonacci number. Return: This Fibonacci number in the function name. */ long fib(int n) { if ((n == 0) || (n == 1)) // stopping cases return 1; else // recursive case return fib(n - 1) + fib(n - 2); }

kept2heart 2009-04-26 22:04:12

Thanks eXceed69 looking at your code helped me figure out what I did wrong in my code


Add comment


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





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month