C++ program to print Fibonacci triangle

Share

The program prints Fibonacci triangle. A Fibonacci series is a series where each number is the sum of  two immediate preceding numbers. For example , 0,1,1,2,3,5 …. and so on.

C++ program to print Fibonacci triangle

Program:

#include <iostream>

using namespace std;

int main ()
{
  int num1 = 0, num2 = 1, totalElements, nextNum;
  totalElements = 4;
  num1 = 0;
  num2 = 1;
     cout << "***************** Fibonacci triangle *************" << endl;
  for (int i = 0; i <= totalElements; i++)
    {
      for (int j = 0; j < i; j++)
  {
    nextNum = num1 + num2;
    cout << " " << num2;
    num1 = num2;
    num2 = nextNum;
  } 
  cout << endl;
    } 
    return 0;
}

Output:

***************** Fibonacci triangle *************                                                                     

                                                                                                                       

 1                                                                                                                     

 1 2                                                                                                                   

 3 5 8                                                                                                                 

 13 21 34 55

This post was last modified on September 29, 2020

Rupesh Rawat

Published by
Rupesh Rawat
Tags: C c program C++ program to print Fibonacci triangle fibonacci triangke program