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 = 5fiboTriangle(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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to print a Hollow Triangle inside a Triangle
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Sum of numbers in the Kth level of a Fibonacci triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to print first n Fibonacci Numbers | Set 1
- C++ Program to print Fibonacci Series using Class template
- C Program to print Fibonacci Series without using loop
- C Program to print Floyd's triangle
- Program to print modified Binary triangle pattern
- Print Fibonacci Series in reverse order
- Print first n Fibonacci Numbers using direct formula
- Print Fibonacci sequence using 2 variables
- 3 Different ways to print Fibonacci series in Java
- Program for Fibonacci numbers
- Python Program for n-th Fibonacci number
- C Program for Fibonacci numbers
- C/C++ Program for nth multiple of a number in Fibonacci Series
- Time complexity of recursive Fibonacci program
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1

