Alternate Fibonacci Numbers in C++



The Fibonacci sequence starts from 0 and 1, and each number is the sum of the previous two. In this problem, we are given a number n, and we need to print the first n numbers from the Fibonacci series, but only the numbers at alternate positions (like 0th, 2nd, 4th, and so on).

Let's look at some example scenarios to understand it clearly:

Scenario 1

Input: n = 7
Output: 0 1 3 8
Explanation: 
The first 7 Fibonacci numbers are 0 1 1 2 3 5 8. 
If we pick alternate numbers (index 0, 2, 4, 6), 
we get 0 1 3 8.

Scenario 2

Input: n = 5
Output: 0 1 3
Explanation: 
The first 5 Fibonacci numbers are 0 1 1 2 3.
If we pick alternate numbers (index 0, 2, 4), 
we get 0 1 3.

Printing Alternate Fibonacci numbers

We can print alternate Fibonacci numbers in C++ using these methods:

Using Recursion and Index Check

Recursion means a function calling itself with smaller inputs until it reaches a base case. To print an alternate Fibonacci series, our recursive function should return the Fibonacci number at a given index. Then, we loop from 0 to n-1 and print the result only when the index is even.

Example

The following is a C++ program that uses recursion to print Fibonacci numbers at positions 0, 2, 4, and so on.

#include<iostream>
using namespace std;

// Recursive function to get Fibonacci number at a given index
int getFibonacci(int index) {
   if (index == 0) return 0;
   if (index == 1) return 1;
   return getFibonacci(index - 1) + getFibonacci(index - 2);
}

// Print alternate Fibonacci numbers
void showAlternateFibonacci(int count) {
   cout << "Input: " << count << endl;
   cout << "Alternate Fibonacci numbers of " << count << " are: ";
   for (int i = 0; i < count; i++) {
      if (i % 2 == 0)  // Only even indices
         cout << getFibonacci(i) << " ";
      }
   cout << endl;
}

int main() {
   int totalTerms = 10;
   showAlternateFibonacci(totalTerms);
   return 0;
}

The output of the above program is shown below, which displays the alternate Fibonacci numbers:

Input: 10  
Alternate Fibonacci numbers of 10 are: 0 1 3 8 21

Time Complexity: O(2^n) because the function keeps calling itself many times for the same values.

Space Complexity: O(n), because of the depth of the recursive call stack, which goes up to n in the worst case.

Generate All and Print Alternates

Instead of printing the alternate numbers of a Fibonacci series, we can generate Fibonacci numbers using a loop and print only those at even positions (0-based index). We use two variables: first to hold the current Fibonacci number and the second for the next one. At each step, we update both variables and print the value only when the current index is even.

Example

Following is a complete C++ program where we print the alternate Fibonacci numbers (those at even positions) using a loop:

#include<iostream>
using namespace std;

// Print Fibonacci numbers at even positions
void showAlternateFibonacci(int count) {
   int first = 0, second = 1;
   for (int i = 0; i < count; i++) {
      if (i % 2 == 0)  // Print only if index is even
         cout << first << " ";
      int next = first + second;  // Get next number
      first = second;
      second = next;
   }
}

int main() {
   int totalTerms = 10;  // Total numbers to print
   cout<<"Input: "<<totalTerms<<endl;
   cout<<"Alternate fibonacci numbers of "<<totalTerms<<" are: ";
   showAlternateFibonacci(totalTerms);
   return 0;
}

The following is the output of the above program, which displays the alternate Fibonacci numbers:

Input: 10
Alternate Fibonacci numbers of 10 are: 0 1 3 8 21

Time Complexity: O(n) because we go through the loop n times.

Space Complexity: O(1) because we only use a few variables.

Dynamic Programming Approach

We use a bottom-up dynamic programming approach where we build the solution starting from the base cases and store the results to reuse them. This helps avoid repeated calculations.

The following are the steps:

  • First, generate the full Fibonacci Series using an array.
  • Initialize the first two terms and fill the rest using the formula: fib[i] = fib[i - 1] + fib[i - 2].
  • Finally, print only the numbers at even indices (0, 2, 4, ...) from the series to get the alternate Fibonacci numbers.

Example

Below is a complete C++ program where we generate and print alternate Fibonacci numbers using Dynamic Programming:

#include<iostream>
using namespace std;

// Print alternate Fibonacci numbers using dynamic programming
void printAlternateFibonacci(int totalTerms) {
   if (totalTerms <= 0) return;

   int fibonacciSeries[totalTerms];
   fibonacciSeries[0] = 0;
   if (totalTerms > 1) fibonacciSeries[1] = 1;

   // Generate the Fibonacci series
   for (int i = 2; i < totalTerms; i++)
      fibonacciSeries[i] = fibonacciSeries[i - 1] + fibonacciSeries[i - 2];

   // Print values at even positions
   for (int i = 0; i < totalTerms; i += 2)
      cout << fibonacciSeries[i] << " ";
}

int main() {
   int numberOfTerms = 10;  // Total numbers to print
   cout<<"Input: "<<numberOfTerms<<endl;
   cout<<"Alternate fibonacci numbers of "<<numberOfTerms<<" are: ";
   printAlternateFibonacci(numberOfTerms);
   return 0;
}

The following is the output of the above program, which displays the alternate Fibonacci numbers.

Input: 10
Alternate Fibonacci numbers of 10 are: 0 1 3 8 21

Time Complexity: O(n) because we go through the number one by one.

Space Complexity: O(n) because we store all the Fibonacci numbers in an array.

Conclusion

In this article, we covered three ways to print alternate Fibonacci numbers in C++. The recursive method is simple but slow for large inputs, while the second, generate and pick method, is easy to follow but uses extra space. The dynamic programming method is the most efficient and is best suited for handling larger values.

Updated on: 2025-08-04T16:24:15+05:30

681 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements