Program to print Fibonacci Triangle | Function to fill Fibonacci Numbers in f [ ] ; 1 st and 2 nd number of the series are 1 and 1 ; Add the previous 2 numbers in the series and store it ; Fill Fibonacci numbers in f [ ] using fib ( ) . We need N = n * ( n + 1 ) / 2 Fibonacci numbers to make a triangle of height n ; To store next Fibonacci Number to print ; for loop to keep track of number of lines ; For loop to keep track of numbers in each line ; Driver code
function fib ( f , N ) { f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( var i = 3 ; i <= N ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } function fiboTriangle ( n ) { var N = ( n * ( n + 1 ) ) / 2 ; var f = [ ... Array ( N + 1 ) ] ; fib ( f , N ) ; var fiboNum = 1 ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= i ; j ++ ) document . write ( f [ fiboNum ++ ] + " " ) ; document . write ( " " ) ; } } var n = 5 ; fiboTriangle ( n ) ;