Program to print Fibonacci Triangle

Given the value of n(n < 10), i.e, number of lines, print the Fibonacci triangle.
Examples:

Input : n = 5 
Output :
1 
1 2 
3 5 8 
13 21 34 55 
89 144 233 377 610 

Input : n = 7
Output :
1 
1 2 
3 5 8 
13 21 34 55 
89 144 233 377 610 
987 1597 2584 4181 6765 10946 
17711 28657 46368 75025 121393 196418 317811 



The Fibonacci numbers are the numbers in the following integer sequence.

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

    Fn = Fn-1 + Fn-2

with seed values F1 = 1 and F2 = 1.

Below is the implementation of the above pattern :

C++

// C++ Implementation for 
// Fibonacci triangle
#include<bits/stdc++.h>
using namespace std;
  
// function to fill Fibonacci Numbers
// in f[]
void fib(int f[], int N)
{
    // 1st and 2nd number of the
    // series are 1 and 1
    f[1] = 1;
    f[2] = 1;
      
    for (int i = 3; i <= N; i++)
      
        // Add the previous 2 numbers
        // in the series and store it
        f[i] = f[i - 1] + f[i - 2];
}
  
void fiboTriangle(int n)
{
    // Fill Fibonacci numbers in f[] using
    // fib(). We need N = n*(n+1)/2 Fibonacci
    // numbers to make a triangle of height
    // n
    int N = n*(n+1)/2;
    int f[N + 1];
    fib(f, N);
      
    // To store next Fibonacci Number to print
    int fiboNum = 1;
  
    // for loop to keep track of
    // number of lines
    for (int i = 1; i <= n;i++)
    {
        // For loop to keep track of
        // numbers in each line
        for (int j = 1;j <= i;j++)
            cout << f[fiboNum++] << " ";
              
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int n = 5;
    fiboTriangle(n);
    return 0;
}

Java

// Java Implementation for 
// Fibonacci triangle
import java.io.*;
  
class GFG {
      
    // function to fill Fibonacci Numbers
    // in f[]
    static void fib(int f[], int N)
    {
        // 1st and 2nd number of the
        // series are 1 and 1
        f[1] = 1;
        f[2] = 1;
          
        for (int i = 3; i <= N; i++)
          
            // Add the previous 2 numbers
            // in the series and store it
            f[i] = f[i - 1] + f[i - 2];
    }
      
    static void fiboTriangle(int n)
    {
        // Fill Fibonacci numbers in f[] using
        // fib(). We need N = n*(n+1)/2 Fibonacci
        // numbers to make a triangle of height
        // n
        int N = n * (n + 1) / 2;
        int f[]=new int[N + 1];
        fib(f, N);
          
        // To store next Fibonacci 
        // Number to print
        int fiboNum = 1;
      
        // for loop to keep track of
        // number of lines
        for (int i = 1; i <= n; i++)
        {
            // For loop to keep track of
            // numbers in each line
            for (int j = 1; j <= i; j++)
                System.out.print(f[fiboNum++] + " ");
                  
            System.out.println();
        }
    }
      
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        fiboTriangle(n);
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 Implementation for 
# Fibonacci triangle
  
      
# function to fill Fibonacci 
# Numbers in f[]
def fib(f, N) :
      
    # 1st and 2nd number of
    # the series are 1 and 1
    f[1] = 1
    f[2] = 1
      
    for i in range(3, N + 1) :
      
        # Add the previous 2 numbers
        # in the series and store it
        f[i] = f[i - 1] + f[i - 2]
          
  
def fiboTriangle(n) :
      
    # Fill Fibonacci numbers in
    # f[] using fib(). We need 
    # N = n*(n + 1)/2 Fibonacci
    # numbers to make a triangle 
    # of height n
    N = n * (n + 1) // 2
    f =[0] * (N + 1)
    fib(f, N)
      
    # To store next Fibonacci 
    # Number to print
    fiboNum = 1
  
    # for loop to keep track of
    # number of lines
    for i in range(1, n + 1) :
      
        # For loop to keep track of
        # numbers in each line
        for j in range( 1, i + 1) :
          
            fiboNum = fiboNum + 1
            print(f[fiboNum], " ", end = "")
              
        print()
          
# Driver code
n = 5
fiboTriangle(n)
  
# This code is contributed by Nikita Tiwari.

C#

// C# Implementation for 
// Fibonacci triangle
using System;
  
class GFG {
      
    // function to fill Fibonacci Numbers
    // in f[]
    static void fib(int []f, int N)
    {
        // 1st and 2nd number of the
        // series are 1 and 1
        f[1] = 1;
        f[2] = 1;
          
        for (int i = 3; i <= N; i++)
          
            // Add the previous 2 numbers
            // in the series and store it
            f[i] = f[i - 1] + f[i - 2];
    }
      
    static void fiboTriangle(int n)
    {
        // Fill Fibonacci numbers in f[] using
        // fib(). We need N = n*(n+1)/2 Fibonacci
        // numbers to make a triangle of height
        // n
        int N = n * (n + 1) / 2;
        int []f = new int[N + 1];
        fib(f, N);
          
        // To store next Fibonacci 
        // Number to print
        int fiboNum = 1;
      
        // for loop to keep track of
        // number of lines
        for (int i = 1; i <= n; i++)
        {
            // For loop to keep track of
            // numbers in each line
            for (int j = 1; j <= i; j++)
                Console.Write(f[fiboNum++] + " ");
                  
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main()
    {
        int n = 5;
        fiboTriangle(n);
    }
}
  
/*This code is contributed by vt_m.*/

PHP

<?php
// PHP Implementation for 
// Fibonacci triangle
  
// function to fill 
// Fibonacci Numbers
// in f[]
function fib(&$f, $N)
{
    // 1st and 2nd number 
    // of the series are
    // 1 and 1
    $f[1] = 1;
    $f[2] = 1;
      
    for ($i = 3; 
         $i <= $N; $i++)
      
        // Add the previous
        // 2 numbers in the 
        // series and store it
        $f[$i] = $f[$i - 1] + 
                 $f[$i - 2];
}
  
function fiboTriangle($n)
{
    // Fill Fibonacci numbers 
    // in f[] using fib(). We
    // need N = n*(n+1)/2 
    // Fibonacci numbers to make
    // a triangle of height n
    $N = $n * ($n + 1) / 2;
    $f = array();
    fib($f, $N);
      
    // To store next 
    // Fibonacci Number 
    // to print
    $fiboNum = 1;
  
    // for loop to keep track 
    // of number of lines
    for ($i = 1; $i <= $n; $i++)
    {
        // For loop to keep track 
        // of numbers in each line
        for ($j = 1;$j <= $i; $j++)
            echo ($f[$fiboNum++] . " ");
              
        echo("\n");
    }
}
  
// Driver code
$n = 5;
fiboTriangle($n);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

Output:

1 
1 2 
3 5 8 
13 21 34 55 
89 144 233 377 610 


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : manishshaw1




Practice Tags :
Article Tags :

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.7 Average Difficulty : 2.7/5.0
Based on 4 vote(s)






User Actions