Program to print tetrahedral numbers upto Nth term | C ++ program to generate tetrahedral number series ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long findTriangularNumber ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } void printSeries ( int n ) { int prev = 0 ; int curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; cout << curr << " ▁ " ; prev = curr ; } } int main ( ) { int n = 10 ; printSeries ( n ) ; return 0 ; }