
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Fibonacci Numbers Using Recursion in C++
The Fibonacci sequence is a sequence of numbers in which each number is the sum of the two numbers that precede it.
Example
Below is the mathematical example to understand the logic of the Fibonacci number:
Suppose, n = 3, then, F(0) = 0 F(1) = 1 F(2) = F(1) + F(0) = 1 + 0 = 1 F(3) = F(2) + F(1) = 1 + 1 = 2 .... ....
So, Fibonacci numbers from F(0) to F(3): 0, 1, 1, 2.
In this article, we will learn how to implement a C++ program to generate a Fibonacci series using recursion. The following are the different logics to implement it:
Fibonacci Series Using Recursion with If-Else
To perform the Fibonacci number using recursion, follow the logical steps in the if-else statement as follows:
- if(x == 0 or x == 1): This is the initial value set to x as 0 or 1.
- else: Otherwise, return the value of x using the formula of Fibonacci numbers like fib(x-1)+fib(x-2).
Example
Following is the basic recursion of using if-else logic to print the result of Fibonacci series:
#include <iostream> using namespace std; // Recursive function to calculate Fibonacci using if-else int fibonacci(int x) { if (x == 0 || x == 1) { return x; } else { return fibonacci(x - 1) + fibonacci(x - 2); } } int main() { int terms = 10; cout << "Fibonacci Series using recursion and if-else: "; for (int i = 0; i < terms; i++) { cout << fibonacci(i) << " "; } return 0; }
Output
The above program produces the following result:
Fibonacci Series using recursion and if-else: 0 1 1 2 3 5 8 13 21 34
Fibonacci Series Using Recursion with Ternary Operator
The recursive function calculates the Fibonacci number by first checking if n is less than or equal to 1, returning n in that case; otherwise, it returns the sum of fibonacci(n-1) and fibonacci(n-2) to build the sequence.
Example
Following is the illustration of the ternary operator inside of a recursive function to solve the series of Fibonacci numbers.
#include <iostream> using namespace std; // Recursive function using ternary operator int fibonacci(int n) { return (n <= 1) ? n : fibonacci(n - 1) + fibonacci(n - 2); } int main() { int terms = 10; cout << "Fibonacci Series up to " << terms << " terms: "; for (int i = 0; i < terms; i++) { cout << fibonacci(i) << " "; } return 0; }
Output
The above program produces the following result:
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
Fibonacci Series Using Recursion with switch case
Here, we use the same formula as in the first approach (if-else); the only difference is that we use a switch-case statement to represent the formula and generate the result.
Example
Below, the recursive function uses a switch-case structure, where the initial values are handled in specific cases, and the Fibonacci sum formula is applied in the default section to produce the program output.
#include <iostream> using namespace std; // Recursive function for Fibonacci int fibonacci(int n) { switch (n) { case 0: return 0; case 1: return 1; default: return fibonacci(n - 1) + fibonacci(n - 2); } } int main() { int terms = 5; cout << "Fibonacci Series using recursion and switch case: "; for (int i = 0; i < terms; i++) { cout << fibonacci(i) << " "; } return 0; }
Output
The above program produces the following result:
Fibonacci Series using recursion and switch case: 0 1 1 2 3
Fibonacci Series Using Recursion with Function Overloading
In this recursive function, use the overloaded function to display the series up to the nth term, then call the main function to produce the output.
Example
In this example, the recursive function fibonacci() only finds the nth term, whereas an overloaded fibonacci() function prints the Fibonacci series up to 10 terms.
#include <iostream> using namespace std; // Recursive function to get nth Fibonacci number int fibonacci(int n) { return (n <= 1) ? n : fibonacci(n - 1) + fibonacci(n - 2); } // Overloaded function to print the series up to 'n' terms void fibonacci() { int terms = 10; cout << "Fibonacci Series using function overloading: "; for (int i = 0; i < terms; i++) { cout << fibonacci(i) << " "; } cout << endl; } int main() { // Call the overloaded function fibonacci(); return 0; }
Output
The above program produces the following result:
Fibonacci Series using function overloading: 0 1 1 2 3 5 8 13 21 34