Login Register Appointment

Simple and Basic Square (*)Pattern

Lesson 1/21 | Study Time: 15 Min
Course: Pattern Program

****

****

****

****


Solution

Note: Don't jump directly to the solution, try it out yourself first.


Approach

1)Require minimum 2 nested loop

2)Outer loop is for row 

3)Inner loop is for column

4)Print in inner loop

Explanation:

1)The Pattern has 4 rows and each row contains 4 stars(*).

2)To print this pattern , we need two loops:

Outer loop for the rows

Inner loop for the columns(stars in each row).

3)In each iteration of the outer loop, the inner loop prints 4 stars.


Video Solution


//You can adjust the value of rows to generate more or fewer lines of the pattern.


Java

public class Pattern {

    public static void main(String[] args) {

        // Number of rows in the pattern

        int rows = 3;


        // Outer loop for rows

        for (int i = 0; i < rows; i++) {

            // Inner loop for columns

            for (int j=0;j<rows;j++) {

                System.out.print(“*”);

            }

            // Move to the next line

            System.out.println();

        }

    }

}



C++
#include <iostream>
using namespace std;

int main() {

    int rows = 4;  // number of rows


    // Outer loop for rows

    for (int i = 0; i < rows; i++) {

        // Inner loop for columns

        for (int j = 0; j < rows; j++) {

            cout << "*";

        }

        cout << endl;  // Move to the next line after printing a row

    }


    return 0;

}



Python

rows = 4  # number of rows



# Outer loop for rows

for i in range(rows):

    # Inner loop for columns

    for j in range(rows):

        print("*", end="")  # Print stars in the same line

    print()  # Move to the next line after printing a row