Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 11, 2024

program in python to print different patterns using for loop

 Objective: Write a program in python to print different patterns using for loop.


Code:


Pattern

Square Pattern:

rows = 5  # Number of rows and columns in the pattern

for i in range(rows):

    for j in range(rows):

        print("*", end=" ")

    print()

 

Right Triangle Pattern:

rows = 5  # Number of rows in the pattern

for i in range(1, rows + 1):

    for j in range(i):

        print("*", end=" ")

    print()

 

 

Inverted Right Triangle Pattern:

 

rows = 5  # Number of rows in the pattern

 

for i in range(rows, 0, -1):

    for j in range(i):

        print("*", end=" ")

    print()

 

 

 

Pyramid Pattern:

 

rows = 5  # Number of rows in the pattern

for i in range(1, rows + 1):

    print(" " * (rows - i), end="")

    print("* " * i)

 


Hollow Square Pattern:

 

rows = 5  # Number of rows and columns in the pattern

for i in range(rows):

    for j in range(rows):

        if i == 0 or i == rows - 1 or j == 0 or j == rows - 1:

            print("*", end=" ")

        else:

            print(" ", end=" ")

    print()

 

Output:



No comments:

Post a Comment