How to print solid rectangle pattern in java program || STAR PATTERN.

 How to print solid rectangle pattern in java program.


To print a solid rectangle pattern in a Java program, you can use nested loops. Here’s a simple example that demonstrates how to create a solid rectangle of asterisks (*). You can adjust the dimensions of the rectangle by changing the values for width and height.



Example: Solid Rectangle Pattern


 int n = 4;

        int m = 5;


        //outer loop 

        for(int i =1 ; i<=n; i++){

            // inner loop 

            for(int j = 1; j <= m; j++){

               

            

            //cell - (i,j)

            if( i == 1 || j == 1 ||  i == n || j == m){


            

            System.out.print("*");

            }else{


            System.out.print(" ");


        }

    }

    System.out.println("");

}

    }

}


Explanation:

  1. Outer Loop: The first loop ( for(int i =1 ; i<=n; i++)) iterates over the number of rows.
  2. Inner Loop: The second loop  for(int j = 1; j <= m; j++)) iterates over the number of columns in each row.
  3. Printing: Inside the inner loop, System.out.print("* ") prints an asterisk followed by a space to form the rectangle. After the inner loop finishes for a row, System.out.println() moves the cursor to the next line.

output:

***** 
***** 
***** 
*****


CLICK HERE;

youtube video