Printing the Triangle Pattern using last term N | Python3 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 Code
from math import sqrt NEW_LINE def triangle ( n ) : NEW_LINE INDENT k = 2 * n - 2 ; NEW_LINE ch = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) ; NEW_LINE DEDENT k = k - 1 ; NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT print ( ch , end = " ▁ " ) ; NEW_LINE ch += 1 ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def maxHeight ( n ) : NEW_LINE INDENT ans = ( sqrt ( 1 + 8.0 * n ) - 1 ) // 2 ; NEW_LINE return int ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; NEW_LINE triangle ( maxHeight ( N ) ) ; NEW_LINE DEDENT