# Improve your loop and problem-solving skills by solving these patterns Hello friends, Today in the post we will test our looping(for loop) skills to solve some patterns. Pattern solving not only helps in improving our loop skills but also helps in improving our problem-solving skills as well. To get the best out of this post, go through the pattern once try solving it, if you can’t read the approach which I used to solve this pattern and you can refer code at last.

Tip: The common thing about this pattern is that we need to be nested for-loop, the outer loop is for rows and the inner loop is for columns.

I am using node js for coding you can use your preferred programming language.

### Pattern1 —

``````* * * *
* * * *
* * * *
* * * *
``````

Here, we have 4 rows and 4 - columns — 4, So we need a loop for tracking row, and for each row, we need another loop for column and print *.

``````const pattern1 = (n) => {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
process.stdout.write("* ");
}
process.stdout.write("\n");
}
};
``````

### Pattern2—

``````*
* *
* * *
* * * *
``````

Here, we have 4 rows and 4 — columns — 4, and we are printing stars based on the row number eg: 1 row — star, 2 row — 2star, 3 row — 3 star, … So again a nested loop outer loop will execute until n and inner loop only till outer loop variable and print *.

``````const pattern2 = (n) => {
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
process.stdout.write("* ");
}
process.stdout.write("\n");
}
};
``````

### Pattern3 —

``````* * * *
* * *
* *
*
``````

It is basically a reverse of the previous pattern. Here, we need star count based on the row for 1st row — 4 stars in 2nd row 3 stars, and so on. So the outer loop will run up to n and for inner loop will be start from n — i and continue until j >= 1

``````const pattern3 = (n) => {

for (let i = 0; i < n; i++) {
for (let j = n - i; j >= 1; j--) {
process.stdout.write("* ");
}
process.stdout.write("\n");
}
};
``````

### Pattern4 —

``````*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
``````

This pattern is the combination of both pattern3 for the upper part and pattern4 for the lower section. But for the lower section, we are just reducing the row count by 1.

``````const pattern4 = (n) => {
for (let i = 0; i < n; i++) {
for (let j = 0; j <= i; j++) {
process.stdout.write("* ");
}
process.stdout.write("\n");
}

for (let i = 0; i < n - 1; i++) {
for (let j = n - 1 - i; j >= 1; j--) {
process.stdout.write("* ");
}
process.stdout.write("\n");
}
};
``````

### Pattern5 —

``````   *
* *
* * *
* * * *
``````

For this pattern, we have stars based on the row count 1st row — 1 star, 2nd row — 2 stars but here before printing the star we need to add space for 1st row we have n — i — 1(4- 0–1 = 3) so 3 spaces, for 2nd row— 2 spaces, 3rd row — 1 space, 4th row — 0 space.

``````const pattern5 = (n) => {

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (j < n - 1 - i) {
process.stdout.write(" ");
} else {
process.stdout.write("* ");
}
}
process.stdout.write("\n");
}
};
``````

### Pattern6 —

``````   *
* *
*   *
*******
``````

For this hollow type triangle pattern, the total column (totcol)required for rows n is n * 2 -1. So we will print the star when our column index is equal to n —( i + 1) — (4 — (0 + 1) = 3) position from the left and (totcol — 1 — n — (i + 1 = 3)position from the right and last row will be filled with stars, and rest other position will be filled with spaces.

``````const pattern6 = (n) => {

let tot = n * 2 - 1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < tot; j++) {
if (j == n - (i + 1) || j == tot - 1 - (n - 1 - i) || i == n - 1) {
process.stdout.write("*");
} else {
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
};
``````

### Pattern7 —

``````* * * *
*     *
*     *
* * * *
``````

For this hollow rectangle pattern, we have an equal number of rows and columns, and we fill stars only when row number is 1 or n or when the column number is 0 or n — 1.

``````const pattern7 = (n) => {

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i == 0 || j == 0 || j == n - 1 || i == n - 1) {
process.stdout.write("* ");
} else {
process.stdout.write("  ");
}
}
process.stdout.write("\n");
}
};
``````

### Pattern8 —

``````      * * * *
*     *
*     *
* * * *
``````

For a hollow rhombus pattern, we have n rows and n * 2–1 column, for left and right border stars, we will print only when the column number is equal (n — i — 1) position from left and (totcol — i — 1) position from right, adding to this we will fill columns when row number is 0 or n — 1.

``````const pattern8 = (n) => {

let tot = n * 2 - 1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < tot; j++) {
if (
j == n - i - 1 ||
j == tot - i - 1 ||
((i == 0 || i == n - 1) && j > n - i - 1 && j < tot - i - 1)
) {
process.stdout.write("* ");
} else {
process.stdout.write("  ");
}
}
process.stdout.write("\n");
}
};
``````

### Pattern9 —

``````*               *
* *           * *
* * *       * * *
* * * *   * * * *
* * * * * * * * *
* * * *   * * * *
* * *       * * *
* *           * *
*               *
``````

This butterfly like pattern requires 2 nested loops one for upper and one for lower half. For upper half, So we need n rows and 2 * n — 1columns, we need stars when column number less than or equal to current row as well when column number is greater than total column minus current row. For lower half we need n — 1 rows, keeping the total columns same as upper half, and print * when column less than or equal to total row — current row number or when column is greater than equal to total row + current row.

``````const pattern9 = (n) => {
let tot = n * 2 - 1;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= tot; j++) {
if (j <= i || j > tot - i) process.stdout.write("* ");
else process.stdout.write("  ");
}

process.stdout.write("\n");
}
for (let i = 1; i <= n - 1; i++) {
for (let j = 1; j <= tot; j++) {
if (j <= n - i || j >= n + i) process.stdout.write("* ");
else process.stdout.write("  ");
}

process.stdout.write("\n");
}
};
``````

### Pattern10 —

``````* * * * * * * *
* * *     * * *
* *         * *
*             *
*             *
* *         * *
* * *     * * *
* * * * * * * *
``````

If you observe this pattern carefully then it is a reverse of our previous butterfly pattern which means lower half becomes the upper here and upper half becomes lower here. But only change is number of columns would n * 2 and in case of printing * for upper half, for left part condition we are checking column less than equal to n + 1 -i.

``````const pattern10 = (n) => {
let tot = n * 2;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= tot; j++) {
if (j <= n + 1 - i || j >= n + i) process.stdout.write("* ");
else process.stdout.write("  ");
}

process.stdout.write("\n");
}
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= tot; j++) {
if (j <= i || j > tot - i) process.stdout.write("* ");
else process.stdout.write("  ");
}

process.stdout.write("\n");
}
};
``````