How to Print traingle pattern by taking user input

By Super Admin | Dec 19, 2020 | PHP
Share :

https://www.fundaofwebit.com/post/how-to-print-traingle-pattern-dynamically

To print traingle pattern by taking input from user, first we have to store the user input in a variable.

Example: If user gives input as 5, the number of rows and columns in the traingle must be 5.

Code :


user_input = input("enter no of lines")
for x in range(int(user_input)):
    for y in range(int(user_input)) :
        if y <= x :
            print("* \t" ,end="")
    print("")
    


The above python code will give the output as shown below:

enter no of lines

Let us keep the user input as 5.

The output terminal will give you the output as below:

enter no of lines 5
*       
*       *
*       *       *
*       *       *       *
*       *       *       *       *

To print the triangle in a upside-down form, Use the below code:


user_input = input("enter no of lines")
for x in range(int(user_input)):
    for y in range(int(user_input)) :
        if y >= x :
            print("* \t" ,end="")
    print("")


The output will be as shown below :

enter no of lines 5
*       *       *       *       *       
*       *       *       *
*       *       *       
*       *
*

https://www.fundaofwebit.com/post/how-to-print-traingle-pattern-dynamically

Share this blog on social platforms