Rectangle Star Pattern

Print Rectangle Star Pattern using Python

In this post, we will learn how to print a rectangle star pattern with the help of python. This is post under the category Build Concepts in Coding

Approach

Our approach will be simple. We will run two for loops. The first for loop will help us to move top to bottom and the second for loop will help us to move from left to right. So with the help of these two loops we will create a rectangle star pattern.

Algorithm To Print Rectangle Star Pattern

Here is the step-by-step algorithm for better understanding.

  1. Take height from user input.
  2. Take width from user input
  3. Now we run nested for loops
  4. Initialize x=0
  5. First for loop:  repeat steps from 6 to 10 till x is less than height.
  6. Initialize y=0
  7. Second for loop: Repeat steps 8 till y is less than width.
  8. print * (star)
  9. break inner for loop 
  10. print next line
  11. break outer for loop

 

Python Code To Print Rectangle Star Pattern

#function to print pattern
def drawPattern(height, width): 
	#first for loop from go to top to bottom
	for x in range(0, height):
		#second for loop to move left to right
		for y in range(0, width):
			print(' * ', end='')
		print('')
		
height = int(input("Enter the height of rectangle "))
width = int(input("Enter the width of rectangle ")) 
drawPattern(height , width)

Output of the following code will be

Rectangle Star Pattern

If you have any queries regarding this code, comment on your problem below. 

Tags: No tags

2 Responses

Leave A Comment

Your email address will not be published. Required fields are marked *