# C++ Program to Find Fibonacci Numbers using Recursion

C++ProgrammingServer Side Programming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

The following is an example of fibonacci series using recursion.

## Example

Live Demo

#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "Fibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
}
return 0;
}

## Output

Enter the number of terms of series : 15
Fibonnaci Series : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

In the above program, the actual code is present in the function ‘fib’ as follows −

if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}

In the main() function, a number of terms are entered by the user and fib() is called. The fibonacci series is printed as follows.

cout << "Enter the number of terms of series : ";
cin >> x;
cout << "Fibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
}
Updated on 26-Jun-2020 08:41:12