Problem Solving: Printing a Rectangular Pattern
Let's create a rectangular pattern using a nested for loop.
We'll cover the following
Printing a rectangular pattern
In this lesson, we’ll learn to print a rectangle using nested for
loops.
Let’s say we have to print the following shape:
Height: 3
Width: 10
Symbol: -
----------
----------
----------
To print the above rectangle, we will take width
and height
as input. In the above shape, the width is 10
, the height is 3
, and the symbol
is '-'
.
Idea
We will follow the following strategy:
- We’ll print the pattern line-by-line with a total of as many lines as the
height
variable. (3 lines from our example above). For this, we’ll have afor
loop counting from 1 tillheight
.
Get hands-on with 1200+ tech skills courses.