c-plus-plus
  1. c-plus-plus-fibonacci-triangle

C++ Program to Generate Fibonacci Triangle

A Fibonacci triangle is a triangle made of Fibonacci numbers, where each row of the triangle corresponds to a Fibonacci sequence.

Syntax

#include <iostream>
using namespace std;

int main() {
   // fibonacci triangle code
   return 0;
}

Example

#include <iostream>
using namespace std;

int main() {
   int n, i, j, a = 0, b = 1, c;
   cout << "Enter the number of rows: ";
   cin >> n;
   for (i = 1; i <= n; i++) {
      a = 0;
      b = 1;
      cout << b << " ";
      for (j = 2; j <= i; j++) {
         c = a + b;
         cout << c << " ";
         a = b;
         b = c;
      }
      cout << endl;
   }
   return 0;
}

Output

Enter the number of rows: 5

1
1 2
1 2 3
1 2 3 5
1 2 3 5 8

Explanation

In the above example, we have written a program to generate a Fibonacci triangle. It starts by taking the input of the number of rows from the user. Then it uses the nested for loop to generate the triangle. The outer loop is used to loop over the rows, and the inner loop is used to loop over the columns for each row. In each row, we start with a = 0 and b = 1, and then calculate the Fibonacci sequence for that row.

Use

The Fibonacci triangle is a fascinating pattern that has applications in many areas of mathematics. It is useful in analyzing complex patterns and sequences in various fields such as biology, finance, and computer science.

Important Points

  • A Fibonacci triangle is a triangle made of Fibonacci numbers, where each row of the triangle corresponds to a Fibonacci sequence.
  • The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers.
  • The Fibonacci triangle is generated using nested for loops that calculate the Fibonacci sequence for each row.

Summary

In summary, the Fibonacci triangle is a fascinating pattern that has applications in many areas of mathematics. It is generated using nested for loops that calculate the Fibonacci sequence for each row. This program is a good example of how to use for loops to generate complex patterns in C++.

Published on: