Program to print tetrahedral numbers upto Nth term | function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It stores the sum of all previously generated triangular number ; Loop to print series ; Find ith triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series
function findTriangularNumber ( n ) { return ( n * ( n + 1 ) ) / 2 ; } function printSeries ( n ) { var prev = 0 ; var curr ; for ( var i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; document . write ( curr + " " ) ; prev = curr ; } } var n = 10 ; printSeries ( n ) ;