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.
- Take height from user input.
- Take width from user input
- Now we run nested for loops
- Initialize x=0
- First for loop: repeat steps from 6 to 10 till x is less than height.
- Initialize y=0
- Second for loop: Repeat steps 8 till y is less than width.
- print * (star)
- break inner for loop
- print next line
- 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
If you have any queries regarding this code, comment on your problem below.
Related Posts:
- Comparison Between Java And Python Programming Languages.
- Pyramid Star Pattern Using Python
- Print Right Angled Triangle Star Pattern
- Binary Search - Leet Code
- Kadane's Algorithm /Largest Sum Contiguous Subarray
- How To Build Concepts In Coding
- Check for Binary Search Tree
- Tree Data Structure - Python
- Get and store data in mysql using python api
- Create GET and POST api in python using flask
Please share the code to print triangle and pyramid also.
Will share it soon