Printing the Triangle Pattern using last term N | C ++ code for printing the Triangle Pattern using last term N ; Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void triangle ( int n ) { int k = 2 * n - 2 ; int ch = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) cout << " ▁ " ; k = k - 1 ; for ( int j = 0 ; j <= i ; j ++ ) { cout << ch ++ << " ▁ " ; } cout << endl ; } } int maxHeight ( int n ) { return ( ( ( int ) sqrt ( 1 + 8.0 * n ) ) - 1 ) / 2 ; } int main ( ) { int N = 9 ; triangle ( maxHeight ( N ) ) ; return 0 ; }