In this tutorial, we will learn about the Fibonacci sequence in C++ i.e. how to write a program to find Fibonacci sequence using C++.

## Quick Info:ðŸ’¡

â†ª The Fibonacci sequence is a series where the next term is the sum of previous two terms.

â†ª For example, if two terms are 0&1 then, the fibonacci sequence can be as: 0, 1, 1, 2, 3, 5, 8, 13, 21â€¦.

â†ª Here, we will find the fibonacci series upto certain number to display its output on the output console.

Fibonacci series of a number can be performed in two ways:

- Without Recursion
- With Recursion

## Fibonacci Sequence Without Recursion

Fibonacci sequence without recursion is a simple program to find it.

Simply, the user will be asked to enter a positive integer then, the Fibonacci sequence is computed and displayed to a user.

### Example:

```
#include<iostream.h>
#include<conio.h>
void main(){
int fibo=1, first_number, second_number;
cout<<"enter the first number:=";
cin>>first_number;
cout<<"enter the second number:=";
cin>>second_number;
while(fibo<=5)
{
fibo=first_number+second_number;
first_number=second_number;
second_number=fibo;
cout<<fibo<<"\t";
}
getch();
}
```

```
//Output:
enter the first number:=0
enter the second number:=1
1 2 3 5 8
```

### Working:

## Fibonacci Sequence With Recursion

Recursion (Recursive function) is defined as â€“ the function calls itself repeatedly unless the code included in the body gets terminated.

And, this is a program to find fibonacci series using recursion.

* Note:* Always remember that execution starts from the main body only.

### Example:

```
#include<iostream.h>
#include<conio.h>
int fibo(int n)
{
if((n==0)||(n==1))
return 1;
else
fibo(n-1)+fibo(n-2);
}
void main(){
int n;
cout<<"enter the value of n:=";
cin>>n;
cout<<"fibonacci sequence is:\n";
cout<<fibo(n)<<endl;
getch();
}
```

```
//Output:
enter the value of n:=5
fibonacci sequence is:
8
```

### Working:

** Note:** Due to recursion, for each function call, an entry is first created in the stack frame and are then executed in a LIFO manner(i.e. Last In First Out).

*Want more programs on recursion?*

*Just Visit: Factorial, *

Well, I hope it really helped you to know C++ better.

Attention readerâš Donâ€™t stop learning now.

Just stay with us to get more such exciting codes to explore more in C++.

### View related posts:

- C++ Program to Add Two Matrices (Multi-dimensional Array)
- C++ Program to Add Two Numbers
- C++ Program to Calculate the Simple Interest
- C++ Program to Check Armstrong Number
- C++ Program to Check Palindrome Number
- C++ Program to Check Whether a Character is Vowel or Consonant
- C++ Program to Demonstrate the Arithmetic Operators
- C++ Program to Demonstrate the Assignment Operator
- C++ Program to Display Multi-Dimensional Array
- C++ Program to Display Single Dimensional Array
- C++ Program to Find Factorial of a Number
- C++ Program to Find Out Product of Digits
- C++ Program to Find Out Sum of Digits
- C++ Program to Find Out the Area of Square
- C++ Program to Find the Fibonacci Sequence
- C++ Program to Find the Largest Among Three Numbers
- C++ Program to Reverse a Number
- C++ Program to Swap Two Numbers
- C++ Program to Transpose the Matrix (2-D Array)

## Leave a Reply