C++ Program to print Fibonacci Series using Class template
Given a number n, the task is to write a program in C++ to print the n-terms of Fibonacci Series using a Class template
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: n = 2 Output: 0, 1 Input: n = 9 Output: 0, 1, 1, 2, 3, 5, 8, 13, 21
- Create a class for the Fibonacci Series
- Take the first two terms of the series as public members a and b with values 0 and 1, respectively.
- Create a generate() method in this class to generate the Fibonacci Series.
- Create an object of this class and call the generate() method of this class using that object.
- The Fibonacci Series will get printed.
Below is the implementation of the above approach:
0 1 1 2 3 5 8 13 21