The Fibonacci sequence can be implemented using recursion. An illustration of a recursion tree is shown below:
An additional check for an even number can be combined with the same algorithm to print only even Fibonacci numbers.
#include <iostream>using namespace std;// Recursive algorithm:int evenFibonacci(int n){if(n == 0 || n == 1)return n;return evenFibonacci(n - 1) + evenFibonacci(n - 2);}int main() {// The number of even Fibonacci numbers to print:int n = 5;int x;for(int i = 0; n > 0; i++){x = evenFibonacci(i);// Check if the number returned is even:if(x % 2 == 0){cout << x << " ";n--;}}return 0;}
Free Resources