Star Patterns (c++) (Part-1)

Detailed view of HTML and CSS code on a dark screen, representing modern web development.
Spread the love

Through the implementation of nested loop, pattern can be printed. Nested loop – loop inside loop. There are many and different patterns that can be printed. But at first, you should have strong command on looping structure and logic behind it. After this, you can print any patterns easily. Here firstly, lets find out the logic behind the patterns.

here, we know star patterns are in matrix form (combination of rows and columns).

Number of Rows (R1, R2, R3, R4) defines the number of loops the programs need. Here, in the above image, 4 rows are there. So program will run for 4 times. It is always the outer loop.

Columns ( C1, C2, C3, C4) is all about the what to print and how many times does it need to repeat.

Printing the patterns is like accessing the elements of matrix. Like R1C1, R1C2, R1C3……..

so rows remain the same till the elements in each column of that row is filled. That is why, row acts as outer loop and column as inner loop.

so lets do some problems:

1. 

here, in question there are 4 rows. So outer i ranges from 0-4( 4 is not involved) i.e 4 times.

here, inner loop j prints the star. In each row (0-3) four elements is going to be printed. if you wanted to print the stars  for 3,2,7, or any times then just manage the inner loop range as per your requirement.

2. 

note: include the header files

here, we have just changed the inner loops to obtain the require patterns. 

explanation:

3. 

note: apply as above explanation


Spread the love

Leave a Comment

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

Scroll to Top