Sum of Fibonacci numbers at even indexes upto N terms | C ++ Program to find sum of even - indiced Fibonacci numbers ; Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Driver program to test above function ; Get n ; Find the even - indiced sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateEvenSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ 2 * n + 1 ] ; fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ; int sum = 0 ; for ( int i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } int main ( ) { int n = 8 ; cout << " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " << n << " ▁ terms : ▁ " << calculateEvenSum ( n ) << endl ; return 0 ; }