- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- C++ program to find Nth Non Fibonacci Number
- Program to find Nth Fibonacci Number in Python
- Program to find last two digits of Nth Fibonacci number in C++
- Program to find nth Fibonacci term in Python
- Java Program to Find Even Sum of Fibonacci Series till number N
- Swift Program to Find Sum of Even Fibonacci Terms Till number N
- Program to find nth ugly number in C++
- Write a C# function to print nth number in Fibonacci series?
- Program to find Fibonacci series results up to nth term in Python
- Python Program for nth multiple of a number in Fibonacci Series
- Java Program for nth multiple of a number in Fibonacci Series
- C program to find Fibonacci series for a given number
- Find the Nth Even Length Palindrome using C++
- Find nth Hermite number in C++
- Returning the nth even number using JavaScript
