C++ Program to print Fibonacci Series using Class template Last Updated : 23 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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, …….. Examples: Input: n = 2 Output: 0, 1 Input: n = 9 Output: 0, 1, 1, 2, 3, 5, 8, 13, 21 Approach: Create a class for the Fibonacci SeriesTake 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: CPP // C++ Program to print Fibonacci // Series using Class template #include <bits/stdc++.h> using namespace std; // Creating class for Fibonacci. class Fibonacci { // Taking the integers as public. public: int a, b, c; void generate(int); }; void Fibonacci::generate(int n) { a = 0; b = 1; cout << a << " " << b; // Using for loop for continuing // the Fibonacci series. for (int i = 1; i <= n - 2; i++) { // Addition of the previous two terms // to get the next term. c = a + b; cout << " " << c; // Converting the new term // into an old term to get // more new terms in series. a = b; b = c; } } // Driver code int main() { int n = 9; Fibonacci fib; fib.generate(n); return 0; } Output:0 1 1 2 3 5 8 13 21 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program to print Fibonacci Series using Class template M me19btech11003 Follow Improve Article Tags : Algorithms Mathematical C++ Programs Articles Programming Language C++ DSA Fibonacci +4 More Practice Tags : CPPAlgorithmsFibonacciMathematical Similar Reads Program to find Nth term of the series 2, 4, 3, 4, 15... Given a number N. The task is to write a program to find the Nth term in the below series as follows: 2, 4, 3, 4, 15... Examples: Input: N = 5 Output: 15 Explanation: For N = 5, Nth term = ( N * ( (N%2) + (N%3) ) = ( 5 * ( (5%2) + (5%3) ) = ( 5 * ( 1 + 2 ) = 15Input: N = 4 Output: 4 Generalized Nth 4 min read Split a Numeric String into Fibonacci Sequence Given a numeric string S representing a large number, the task is to form a Fibonacci Sequence of at least length 3 from the given string. If such a split is not possible, print -1. Examples: Input: S = â5712â Output: 5 7 12 Explanation: Since 5 + 7 = 12, the splits {5}, {7}, {12} forms a Fibonacci 10 min read Generating large Fibonacci numbers using boost library In our previous post on fibonacci series, we have seen many approaches to generate fibonacci numbers. In this approach, we shall be generating fibonacci numbers with the help of boost library. The program simply uses the "boost/multiprecision/cpp_int.hpp" boost library in which big_int is defined. T 5 min read How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read C++ Program To Find Sum of Fibonacci Numbers at Even Indexes Upto N Terms Given a positive integer N, the task is to find the value of F2 + F4 + F6 +.........+ F2n upto N terms where Fi denotes the i-th Fibonacci number.The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...... Examples: Input: n = 5 Outpu 4 min read Like