Java Program to Print Hollow Rectangle Star Pattern

Program to Print Hollow Rectangle Star Pattern

Print Hollow Rectangle Star Pattern in Java

In this article we are going to see how to print Hollow Rectangle star pattern.

Example-1

we entered row value as 6
column value as 10
**********
*            *
*            *
*            *
*            *
**********
Example-2

we entered row value as 3
column value as 20
********************
*                           *
********************

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach

  • Enter total row and store it in an integer variable say row.
  • Enter total column and store it in an integer variable say column.
  • Take one inner loop to print the column values.
  • Keep iterating and print the values.

JAVA Code:

 

Method-1 : Static Star Character

import java.util.Scanner;
public class Main{

public static void main(String[] args)
{
    int r, c, row, column;
    Scanner sc= new Scanner(System.in);
    // ask user to enter row and column value
    System.out.print("Enter number of rows = ");
    row=sc.nextInt();
    System.out.print("Enter number of columns = ");
    column=sc.nextInt();
    // loop to iterate over rows
    for(r=1; r<=row; r++)
    {
        // loop to iterate over columns and print values
        for(c=1; c<=column; c++)
        {
            // if r=1 or row OR c=1 or column print a star symbol
            // else print a space
            if(r==1 || r==row || c==1 || c==column)
            {
                System.out.print("*");
            }
            else
            {
                System.out.print(" ");
            }
        }

        // move to next line or row
        System.out.println("");
    }
}
}


Output:

Enter number of rows = 5
Enter number of columns = 12
************
*               *
*               *
*               *
************

Method-2 : User Input Character

import java.util.Scanner;
public class Main{

public static void main(String[] args)
{
    int r, c, row, column;
    char ch;
    Scanner sc= new Scanner(System.in);
    // ask user to enter row and column value
    System.out.print("Enter number of rows = ");
    row=sc.nextInt();
    System.out.print("Enter number of columns = ");
    column=sc.nextInt();
    System.out.print("Enter any random character = ");
    ch=sc.next().charAt(0);
    // loop to iterate over rows
    for(r=1; r<=row; r++)
    {
        // loop to iterate over columns and print values
        for(c=1; c<=column; c++)
        {
            // if r=1 or row OR c=1 or column print a star symbol
            // else print a space
            if(r==1 || r==row || c==1 || c==column)
            {
                System.out.print(ch);
            }
            else
            {
                System.out.print(" ");
            }
        }

        // move to next line or row
        System.out.println("");
    }
}
}


Output:

Enter number of rows = 5
Enter number of columns = 12
Enter any random character = #
############
#                      #
#                      #
#                      #
############

Explanation:

Let’s understand the program with detailed explanation.

Let we have taken row as 5 and column as 12.

Iteration-I

r=1 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It checks if r= 1 or row OR c= 1 or column. If the condition satisfies print star symbol else print a space i.e. total 12 stars and 0 spaces are printed. Inner loop condition fails, so control come out of inner loop.

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

Iteration-II

r=2 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It checks if r= 1 or row OR c= 1 or column. If the condition satisfies print star symbol else print a space i.e. total 2 stars and 10 spaces are printed. Inner loop condition fails, so control come out of inner loop.

*          *

Iteration-III

r=3 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It checks if r= 1 or row OR c= 1 or column. If the condition satisfies print star symbol else print a space i.e. total 2 stars and 10 spaces. Inner loop condition fails, so control come out of inner loop.

*          *

Iteration-IV

r=4 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It checks if r= 1 or row OR c= 1 or column. If the condition satisfies print star symbol else print a space i.e. total 2 stars and 10 spaces. Inner loop condition fails, so control come out of inner loop.

*          *

Iteration-V

r=5 (passed through first for loop condition) which will execute till r<=row. It will enter inner loop which iterates from c=1 till c<=column. It checks if r= 1 or row OR c= 1 or column. If the condition satisfies print star symbol else print a space i.e. total 12 stars and 0 spaces. Inner loop condition fails, so control come out of inner loop.

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

Now r=6, so first for loop condition fails i.e. r<=row. And no more for loop will be executed. And last we will see a pattern like this on the output screen.

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

C Code:

#include <stdio.h>

int main()
{
    int r, c, row, column;
    printf("Enter number of rows = ");
    scanf("%d", &row);
    printf("Enter number of columns = ");
    scanf("%d", &column);
    for(r=1; r<=row; r++)
    {
        for(c=1; c<=column; c++)
        {
            if(r==1 || r==row || c==1 || c==column)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}
Output:

Enter number of rows = 5
Enter number of columns = 12
************
*               *
*               *
*               *
************

C++ Code:

#include<iostream>

using namespace std;

int main()
{
    int r, c, row, column;
    cout<<"Enter number of rows = ";
    cin>>row;
    cout<<"Enter number of columns = ";
    cin>>column;
    for(r=1; r<=row; r++)
    {
        for(c=1; c<=column; c++)
        {
            if(r==1 || r==row || c==1 || c==column)
            {
                cout<<"*";
            }
            else
            {
                cout<<" ";
            }
        }
        cout<<"\n";
    }

    return 0;
}

Output:

Enter number of rows = 5
Enter number of columns = 12
************
*               *
*               *
*               *
************

Related Java Star Pattern Programs: