Coders Packet

How to print a hollow pattern in JAVA.

By Kartik Rakesh Khandelwal

Printing hollow pattern using JAVA programming language while taking a number of rows and columns as an input.

The purpose is to print a hollow pattern while taking a number of rows and columns as input from the user in the JAVA programming language.

We are taking rows and column numbers as input from users. 

"i" and "j" are iterators of the loop. 

How the loops are working: When the value of  i=1 or i equals the number of rows or when the value of j=1 or j equals the number of columns,
then "*" will be printed, otherwise " "(Blank space) will be given. After each iteration of a row, the next line will be printed.

import java.util.*;

public class hollowPattern {

  public static void main(String[] args) {
  
    int col, row,i,j;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Rows: ");
    row = sc.nextInt();
    System.out.println("Enter Columns: ");
    col = sc.nextInt();
    
    for(i=1;i<=row;i++) {
      for(j=1;j<=col;j++) {
        if(i==1 || i==row || j==1 || j==col) {
          System.out.print("* ");
        }
        else {
          System.out.print("  ");
        }
      }
      System.out.println();
    }
    
  }

}

printing Hollow Pattern

Download project

Reviews Report

Submitted by Kartik Rakesh Khandelwal (kkwal27)

Download packets of source code on Coders Packet