In this example, we will learn to create a diamond shape using n numbers in python. Here, we will develop the shape of a diamond with 2n rows.

Our main motive is to design a diamond shape pattern using star symbols and print across the lines.

    Algorithm:

  1. Input the number of row that is needed to develop a diamond shape pattern
  2. Use for loop with range(n) 
  3. Use another for loop with range(1,int((n/2))-i+3) 
  4. Print(sep=” “,end=” “)
  5. Loop(3) ends
  6. Using for loop for range(1,i+2)
  7. Print(“*”, end=” “)
  8. Loop(6) ends
  9. Print the space and loop (2) ends
  10. For lower half of the diamond we use for loop with range(n)
  11. Use another for loop with range(1,5-(int((n/2))-i+3)+2)
  12.  Print(sep=” “,end=” “)
  13. Loop(11) ends
  14. Use for loop with range(1,5-i)
  15. Print(“*”, end=” “)  and loop(14) ends
  16. Print the space and loop(10) ends
  17. Diamond pattern is shown on the screen
  18. Exit 

Code:

n=int(input("enter the number of rows"))

for i in range(n):

    for j in range(1,int((n/2))-i+3):

        print(sep=" ",end=" ")

    for k in range(1,i+2):

        print("*", end=" ")

        

    print()

for i in range(n):

    for j in range(1,5-(int((n/2))-i+3)+2):

        print(sep=" ",end=" ")

    for k in range(1,5-i):

        print("*", end=" ")

    print()

Output:



       * 

   * * 

  * * * 

 * * * * 

* * * * * 

 * * * * 

  * * * 

   * * 

    *

 

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019