Program to print Fibonacci Triangle


A Fibonacci series is found in every row of the Fibonacci triangle. What is a fibonacci series?

In the Fibonacci series, each digit equals the sum of the two integers before it. This series' first two digits are 1 and 1.

The next elements in the series are computed as the sum of two numbers previous to it. The Fibonacci series is produced as 1+1=2, 2+3=5, 3+5=8, 8+13=21, 13+21=34 and so on.

Likewise, the Fibonacci triangle series goes like 1,1,2,3,5,8,13,21,34,55…

Problem Statement

Implement a program to print the Fibonacci Triangle.

Appproach

In a Fibonacci triangle, the first row contains the first element. That is the number 1. The second row contains the previous row element (first row) as well as the sum. Since there is only one element, we take that itself as the sum. That is 1 and 1. Moving the third row, it contains previous row elements (second row) and the sum of the last two elements in that row. That is 1,1,2. Here 2 is the sum. Coming onto the fourth row, it contains previous row elements (third row) and the sum of the last two elements in that row. That is 1,1,2,3. Here 3 is the sum. Taking the fifth row, it contains previous row elements (fourth row) and the sum of the last two elements in that row. That is 1,1,2,3,5. Here 5 is the sum. Taking the sixth row, it contains previous row elements (fifth row) and the sum of the last two elements in that row. That is 1,1,2,3,5,8. Here 8 is the sum. The triangle expands like this.

Example 1

Let the number of rows we want to print be 5.

The Fibonacci triangle we obtain in this case is shown below.

1
1  1
1  1  2            (1+1=2)
1  1  2  3         (1+2=3)
1  1  2  3  5      (2+3=5)

Example 2

Let the number of rows we want to print be 8.

The fibonacci triangle we obtain in this case is shown below.

1
1  1
1  1  2                      (1+1=2)
1  1  2  3                   (1+2=3)
1  1  2  3  5                (2+3=5)
1  1  2  3  5  8             (3+5=8)
1  1  2  3  5  8  13        (5+8=13)
1  1  2  3  5  8  13  21   (8+13=21)

Algorithm

STEP 1: Set the triangle's height. (In this case we specified the height as 5).

STEP 2: Run two loops. The inner loop prints the triangle's body while the outside loop points to each triangle row.

STEP 3: During the specified height of times, the outer loop shall run. The inner loop would travel through the outer loop a varying number of times on each iteration. It will run through once on the initial row, twice for the second row, etc.

STEP 4: The Fibonacci sequence will be printed within the inner loop.

STEP 5: The first two digits will be displayed as 1 and 1.

STEP 6: The preceding two numbers will continue to be added to get the value for the remaining numbers.

STEP 7: Each time the outer loop iterates; a new line will be printed to continue moving to the triangle's subsequent line.

Implementation

C Program to print Fibonacci triangle

Example

#include<stdio.h>   
#include<stdlib.h>
int main()
{  
   int n1=0,n2=1,n3,n=5,i,j;   
   for(i=1;i<=n;i++)   
   {   
      n1=0;   
      n2=1;   
      printf("%d\t",n2);   
      for(j=1;j<i;j++)   
      {   
         n3=n1+n2;   
         printf("%d\t",n3);   
         n1=n2;   
         n2=n3;   
   
      }   
      printf("\n"); 
   }   
return 0;  
}

Output

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

Conclusion

Likewise, we can find the Fibonacci triangle number by inputting any value to n.

The challenge of printing the Fibonacci triangle is resolved in this article. Here C programming codes to print Fibonacci triangles are provided. In this program, we are giving input for the limit for the Fibonacci triangle, and we are printing the Fibonacci series for the given number of times or the limit provided.

Updated on: 23-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements