# Compute Nth Fibonacci Number

“** Compute Nth Fibonacci Number**” is similar to problem of printing fibonacci series upto Nth Term, the only difference between is that, in this problem we need to compute and print only Nth Term of the Fibonacci Series, while, in other problem we need to print the fibonacci series up to Nth term.

Here, we are given a number ‘n’, entered by user and Our task is to compute the nth Fibonacci number.

Fibonacci series: 0, 1, 1, 2, 3, 5, 8…

The first two terms of Fibonacci series are fixed and are 0 and 1. All the succeeding numbers of the Fibonacci series are sum of preceding two numbers.

Like, First term = 0, Second Term = 1

Third Term = Second Term(1) + First Term(0) = 1

Fourth Term = Third Term(1) + Second Term(1) = 2

Fifth Term = Fourth Term(2) + Third Term(1) = 3

.

.

.

So on…

Formula to Compute Nth Fibonaccinumber:F(n) = F(n-1) + F(n-2)

**Example (Compute Nth Fibonacci number):**

N = 5INPUT:3OUTPUT:N = 6INPUT:5OUPUT:

**C++ Program to compute nth fibonacci number is as follows:**

```
/* C++ Program to compute nth fibonacci number */
#include<bits/stdc++.h>
using namespace std;
int main()
{
//Scan the number
int n;
cout<<"Enter the number:";
cin>>n;
// Computing the fibonacci
int first = 0;
int second = 1;
int nth;
for(int i = 3; i <= n ; i++)
{
nth = first + second;
first = second;
second = nth;
}
// Printing the fibonacci number
if(n==1)
{
cout<<"The nth fibonacci number is "<<first;
}
else if(n==2)
{
cout<<"The nth fibonacci number is "<<second;
}
else
{
cout<<"The nth fibonacci number is "<<nth;
}
}
```

Enter the number:5 3OUTPUT:

**Related Posts:**

- Program to find Power of the Number.
- Program to find Quotient and Remainder.
- Program to find largest amongst three numbers.
- Program to find factorial of a number.
- Program to find GCD of two numbers.
- Program to find LCM of two numbers.
- Program to check whether entered number is odd or even.
- Program to check whether entered number is prime number or not.
- Program to check whether entered number is palindrome or not.
- Program to check whether entered number is Armstrong Number or Not.
- Program to convert binary number to octal number.
- Program to convert binary number to decimal number.
- Program to convert binary number to hexadecimal number.
- Program to convert octal number to binary number.
- Program to convert octal number to decimal number.
- Program to convert octal number to hexadecimal number.
- Program to convert decimal number to binary number.
- Program to convert decimal number to octal number.
- Program to convert decimal number to hexadecimal number.
- Program to convert hexadecimal number to binary number.