# Alternate Fibonacci Numbers in C++

C++Server Side ProgrammingProgramming

Fibonacci Number is defined as a sequence of numbers that starts with two fixed numbers, generally, o,1 or 1, 1 and the successive elements of the sequence are the sums of the previous two numbers of the sequence.

For example, Fibonacci series till 8 elements is 0,1,1,2,3,5,8,13,21,34,55,89.

Now, let's generalize this series. Here, the value of the nth term is equal to the sum of (n-1)th and (n-2)th term. So, let's get the mathematical derivation of the formula for the nth term of the Fibonacci series.

Tn = Tn-1 + Tn-2

Using this formula to find out the 5th term of the Fibonacci series, we have the 3rd and 4th term given.

T5 = T4 + T4

T5 = 3 + 5 = 8.

Alternate Fibonacci series is a Fibonacci series that has values the same as the Fibonacci series but alternate elements are to be printed in the series. For example, the first 4 elements of the alternate Fibonacci series are 0, 1 , 3, 8.

To create a program to print alternate Fibonacci series, we will use the formula and for every element of the series and then print only alternate values of the series.

## Algorithm

Step 1 : Initialize the first two values of the series n1 = 0 and n2 = 1.
Step 2 : loop from i = 2 to n and follow 3-5 :
Step 3 : next element is n3 = n1 +n2
Step 4 : n1 = n2 and n2 = n3
Step 5 : if i%2 == 0 : print n3

## Example

Live Demo

#include <iostream>
using namespace std;
int main(){
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements to be present in the series: ";
cin>>number;
cout<<"Alternate Fibonacci Series is : ";
cout<<n1<<" ";
for (i=2;i<(number*2);++i){
n3=n1+n2;
n1=n2;
n2=n3;
if(i%2==0)
cout<<n3<<" ";
}
return 0;
}

## Output

Enter the number of elements to be present in the series: 4
Alternate Fibonacci Series is : 0 1 3 8
Published on 16-Oct-2019 10:28:21