Program of triangle patterns

In this page you are going to learn how to program triangle patterns.

In python if we want to print single dimensional(ex: line), one for loop is enough.

for i in range(0,5):

     print("*" , end="")

                Output : *****

If we want print two dimensional we want to use inner for loop. 'for loop' in 'for loop'. total two for loops

In below codes we are going to print the following six different triangle pattern with help of inner for loop

 

 

 

 

 

 

 

 

 

 

Triangle pattern 1 :

Python code

tri_size = int(input("Enter size of triangle : "))

for i in range(0,tri_size+1):

for j in range(0,i):

print("*",end="")

print("")

Output :

*
**
***
****
*****

 Note: I am passing tri_size as 5, so in the output it is 5 rows 

Triangle pattern 2 :

Python code:

tri_size = int(input("Enter size of triangle : "))
for i in range(0,tri_size):
    for j in range(0,tri_size):
        if(j>=tri_size-i-1):
            print("*", end="")
        else : print(" ", end="")
    print("")

Output :

     *
    **
   ***
  ****
 *****

 Note: I am passing tri_size as 5, so in the output it is 5 rows 

Triangle pattern 3 :

Python code:

tri_size = int(input("Enter size of triangle : "))
for i in range(tri_size,0,-1):
    for j in range(tri_size,0,-1):
        if(j>tri_size-i):
            print("*", end="")
        else : print(" ", end="")
    print("")

Output :

*****
**** 
***  
**   
*   

 Note: I am passing tri_size as 5, so in the output it is 5 rows 

 

Triangle pattern 4 :

Python code:

tri_size = int(input("Enter size of triangle : "))
for i in range(0,tri_size):
    for j in range(tri_size,0,-1):
        if(j<=tri_size-i):
            print("*", end="")
        else : print(" ", end="")
    print("")

Output :

*****
 ****
  ***
   **
    *   

 Note: I am passing tri_size as 5, so in the output it is 5 rows

Triangle pattern 5 :

Python code:

tri_size = int(input("Enter size of triangle : "))
for i in range(0,tri_size):
    for j in range(0,tri_size+i):
        if(j>=tri_size-i-1):
            print("*", end="")
        else : print(" ", end="")
    print("")

Output :

    *
   ***
  *****
 *******
*********  

 Note: I am passing tri_size as 5, so in the output it is 5 rows

Triangle pattern 6 :

Python code:

tri_size = int(input("Enter size of triangle : "))
k=tri_size-1
for i in range(0,tri_size):
    for j in range(tri_size+k,0,-1):
        if(j<=tri_size+k-i):
            print("*", end="")
        else : print(" ", end="")
    k=k-1
    print("")

Output :

*********
 *******
  *****
   ***
    *

 Note: I am passing tri_size as 5, so in the output it is 5 rows

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform