Q. Write a program to print the following patterns:


(i)
  *   
 * *  
* * *
 * *  
  *  

(ii)
        1    
      212   
    32123  
  4321234
543212345

(iii)

12345
  1234
    123
       12
         1

(iv)

  *  
 * *
*   *
 * *
  *  




Answer :-

(i)

for i in range(6):
    for j in range(6):
        if i < 3 :
            if j >= 2 - i and j <= 2 + i :
                if i % 2 == j % 2 :
                    print(" ", end = "")
                else :
                    print("*", end = "")
            else :
                print(" ", end = "")
        else :
            if j <= 8 - i and j >= i - 4 :
                if i % 2 == j % 2 :
                    print(" ", end = "")
                else :
                    print("*", end = "")
            else :
                print(" ", end = "")
    print()


(ii)

a = "543212345"

for i in range(6):
    for j in range(9):
        if j > 4 - i and j < 4 + i :
            print(a[ j ], end="")
        else :
            print(" " , end = "" )
    print()


(iii)

a = "12345"

for i in range(5):
    print(" " * i + a [ : 5- i ] )


(iv)

for i in range(5):
      for j in range(5):
            if i < 3 :
                  if j == 2 - i or j == 2 + i :
                        print("*" , end="")
                  else :
                        print(" " ,  end = "")
            else :
                  if j == i-2 or j == 6 - i :
                        print("*" , end="")
                  else :
                        print(" " ,  end = "")
      print()

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post