Program to find Nth Even Fibonacci Number in C++


In this problem, we are given an integer value N. Our task is to find Nth Even Fibonacci Number.

Fibonacci Series generates subsequent number by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.

Let’s take an example to understand the problem,

Input : N = 4
Output : 144

Solution Approach

A simple solution to the problem is using the fact that every third number in the fibonacci sequence is even and the sequence of even numbers also follows the recursive formula.

Recursive formula for even Fibonacci sequence is −

Ef(n)= 4Ef(n-1) + Ef(n-2) where Ef(0)=0 and Ef(1)=2

We know that every third fibonacci number is even, thus f(n-3) and f(n-6) both are even. So, we will consider f(n) as the kth element and be denoted as Ef(k). If f(n) is Ef(k), then f(n-3) is the previous even number denoted by Ef(k-1) and thus f(n-6) is the previous of Ef(k-1) that is Ef(k-2).

Thus f(n)=4f(n-3)+f(n-6)

Or, Ef(k)=4Ef(k-1) + Ef(k-2).

Example

Program to illustrate the working of our solution,

#include<iostream>
using namespace std;
int findNthEvenFiboNum(int n){
   if (n < 1)
      return n;
   if (n == 1)
      return 2;
   return ((4 * findNthEvenFiboNum(n-1)) + findNthEvenFiboNum(n- 2));
}
int main (){
   int n = 5;
   cout<<n<<"th even fibonacci number is "<<findNthEvenFiboNum(n);
   return 0;
}

Output

5th even fibonacci number is 610

Updated on: 11-Feb-2022

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements