Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program to print Diamond Star Pattern | PrepInsta

Diamond Star Pattern Printing

 

In this program, we are taking the number of rows as input from the user and store in the variable named n, and then we will use two for loops, one for the upper half and the second for the lower half.

In the first half  ” * ” increases as the number of rows increases and then in the second half “*” decreases as we go down.

Diamond Star Pattern

Algorithm

  • Take number of Rows as input from the user
  • Call diamondStar(n)
diamondStar(rows):
  1. Initialize two variables k1=1 and k2=rows-1
  2. Traverse i from 0 to rows and do step 3 to step 8 (1st Outer Loop for the upper half )
  3. Traverse j from 0 to rows and do step 4 to step 5 (Inner loop to print elements in a particular row)
  4. if j is greater than or equal to k2 print *
  5. else print ” ” blank space.
  6. End of inner loop
  7. Decrement k for each row
  8. Print new line character for each inner iteration
  9. End of 1st outer loop
  10. Traverse i from 0 to rows and do step 11 to step 16 (2nd Outer Loop for the lower half )
  11. Traverse j from 0 to rows and do step 12 to step 13 (Inner loop to print elements in a particular row)
  12. if j is greater than or equal to k1 print *
  13. else  print ” “
  14. End of inner loop
  15. Increment k for each row
  16. Print new line character for each inner iteration
  17. End of 2nd outer loop.

Python Code


#function to print diamon star pattern
def diamondStar(rows):
    #intialize variables k1 and k2
    k1=1
    k2=rows-1
    
    #1St outer loop for each row in the upper half
    for i in range(0,rows):
        #Inner loop to print elements in a particular row
        for j in range(0,rows):
            #if j>=k2 print *
            if j>=k2:
                print(“* “,end=” “)
            #if j<k2 print blank space
            else:
                print(” “,end=” “)
        #decrement k2
        k2-=1
        #print new line character for each row
        print()
        
    #2nd Outer loop for each row in the lower half
    for i in range(0,rows):
         #Inner loop to print elements in a particular row
        for j in range(0,rows):
            #if j>=k1 print *
            if j>=k1:
                print(“* “,end=” “)
            #if j<k1 print blank space
            else:
                print(” “,end=” “)
        #increment k1
        k1+=1
        #print new line character for each row
        print()
 
#input
n=int(input(“Enter the number of Rows : “))
#function call
diamondStar(n)


Output :

Enter the number of Rows : 6

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