Print Fibonacci Series using C++
In programming, the most important thing that matters is how good you are at solving problems. If you know how to write simple programs, then it’s time to move ahead in this game. Constant improvement of your programming ability is the key to becoming a respected programmer. Real-world application-based questions are the ones you must try to solve always. Solving a wide range of problems improves critical thinking ability as well. Well, it’s also like that you are already familiar with number patterns, but what about printing a Fibonacci Series. In today’s article, we will print Fibonacci Series using C++. With the help of this program, you will be able to print amazing patterns of numbers based on addition. Practising these types of questions also helps to get an upper edge in Competitive Programming.
Open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own. This is a simple question, but you might get asked in an interview so make sure to practice it on your own after reading this article.
What’s The Approach?
- We’re going to use a recursive approach for printing the Fibonacci series in C++. As we know the Fibonacci Series starts with
0 1
and the next numbers in this series are the addition of the last two numbers.
- We will consider
n
as the number of Fibonacci series we want to print. We will write functionfib()
that will return Fibonacci number till n. It will call itself recursively until we reach our input n.
C++ Program To Print Fibonacci Series
Input:
n = 9
Output:
34
//Fibonacci Series using Recursion #include<bits/stdc++.h> using namespace std; int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } int main () { int n = 9; cout << fib(n); getchar(); return 0; }
Also Read: How to install Turbo C++ on Windows 11