+1 vote
in Python by
Write A Program in Python to Print the following pattern

1 Answer

0 votes
by
#

      # #

    # # #

  # # # #

# # # # #

Solution –>

       

Code:

def pattern_2(num):

      

    # define the number of spaces

    k = 2*num - 2

  

    # outer loop always handles the number of rows

    # let us use the inner loop to control the number of spaces

    # we need the number of spaces as maximum initially and then decrement it after every iteration

    for i in range(0, num):

        for j in range(0, k):

            print(end=" ")

      

        # decrementing k after each loop

        k = k - 2

      

        # reinitializing the inner loop to keep a track of the number of columns

        # similar to pattern_1 function

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

            print("# ", end="")

      

        # ending line after each row

        print("\r")

  

num = int(input("Enter the number of rows in pattern: "))

pattern_2(num)

Related questions

0 votes
asked Jan 17, 2021 in Python by SakshiSharma
+2 votes
asked Feb 15, 2021 in Python by SakshiSharma
...