Rectangular shaped pattern with letters – C Program




About this program

Till now we have been doing pattern programs on numbers and stars. In this program, we are going to print a rectangular shaped pattern with letters. The output will be of five rows and five columns. and every row will have five same letters, like the first row will have ‘AAAAA’, the second row will have ‘BBBBB’, the third row will have ‘CCCCC’ and so on.

The output of this program will look something like this:

We have already done this rectangular shaped pattern with stars and numbers.

Simple star pattern rectangle

Rectangular shaped pattern with numbers

The logic of this program is same as the above two programs.



Code explanation

This program consists of five rows and five columns. So we need two ‘for..loops’ here. One for columns and one for rows.

Here the outer for..loop is for rows and the inner loop is for columns.

At first, we have taken two char variables i and j. The reason we have taken char variable is that here we are printing characters.

The inner loop

Suppose the value of ‘i‘ is ‘C‘. So the output of the above code will be CCCCC.

At first, ‘A‘ is assigned to the variable j. Then the condition is checked. Is ‘j‘ less than or equal to ‘E‘? If yes, print the value of ‘i‘ which is C.

j‘ is incremented to ‘B‘. Is ‘B‘ less than or equal to E? if yes, print the value of ‘i‘ which is again ‘C‘.

j‘ is incremented to ‘C‘. Is ‘C‘ less than or equal to E? if yes, again print the value of ‘i‘. This process continues until the value of ‘j‘ is less than or equal to E.

So at last, the output will be CCCCC.

Now we need to repeat this with five times. SO we need another for..loop.

To repeat the value of ‘i‘ five times we need this outer loop. So if you run this code you will get the output something like this:

To make it look like a rectangle we just need to break the line after five characters or after the inner loop.

now the output will look like a rectangle.

You can also read the explanation part of the java program to print numbered square pattern.

You May Also Like

1 Comment

Leave a Reply

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