Java Program to Print Hollow Square Star Pattern

Program to Print Hollow Square Star Pattern

In this article we are going to see how to print the  hollow square  star program.

Example-1

When row value=4
*  *  *  *
*         *
*         *
*  *  *  *
Example-2:

When row value=5
*  *  *  *  *
*             *
*             *
*             *
*  *  *  *  *

Now, let’s see the actual program to print it.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print all rows.
  • Take second/inner for loop to print column values.
  • Then go on printing the star symbols according to the iteration  condition i.e.
    if(r == 1 || r == row || c == 1 || c == row) .

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   // taking variable for loop iteration and row .
    int row,r,c,d;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //for loop for rows
  		for(r = 1; r <= row ; r++)
        {
            // printing stars by checking condition 
            for(c = 1; c <= row; c++)
                if(r == 1 || r == row || c == 1 || c == row)
                    System.out.print("* "); 
                else
                    System.out.print("  "); 
            System.out.print("\n"); 
        }	            
    }
}
Output:

Enter rows : 5
*  *  *  *  *
*             *
*             *
*             *
*  *  *  *  *

Method-2 : User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   // taking variable for loop iteration and row .
    int row,r,c,d;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering any character
    System.out.print("Enter character : ");
    char sym= s.next().charAt(0);
    //for loop for rows
  		for(r = 1; r <= row ; r++)
        {
            // printing stars by checking condition 
            for(c = 1; c <= row; c++)
                if(r == 1 || r == row || c == 1 || c == row)
                    System.out.print(sym+" "); 
                else
                    System.out.print("  "); 
            System.out.print("\n"); 
        }	            
    }
}
Output:

Enter rows : 5
Enter character : #
# # # # #
#           #
#           #
#           #
# # # # #

Explanation :

Let’s understand the program by going through the detailed explanation.

We have taken row value as 5.

Iteration-1
r=1 (passes the first for loop condition) because it will execute until r<= row.
second for loop will iterate up to  1 to row time and for each iteration it will check the condition if(r == 1 || r == row || c == 1 || c == row) for each if condition satisfy it will print * and for each if condition fails it will print space  so  star will print  5 time .

*  *  *  *  *

Iteration-2
r=2 (passes the first for loop condition) because it will execute until r<= row.
second for loop will iterate up to  1 to row time and for each iteration it will check the condition if(r == 1 || r == row || c == 1 || c == row) for each if condition satisfy it will print * and for each if condition fails it will print space  so  star will print  2 time .

*               *

Iteration-3
r=3 (passes the first for loop condition) because it will execute until r<= row.
second for loop will iterate up to  1 to row time and for each iteration it will check the condition if(r == 1 || r == row || c == 1 || c == row) for each if condition satisfy it will print * and for each if condition fails it will print space  so  star will print  2 time .

*               *

Iteration-4

r=4 (passes the first for loop condition) because it will execute until r<= row.
second for loop will iterate up to  1 to row time and for each iteration it will check the condition if(r == 1 || r == row || c == 1 || c == row) for each if condition satisfy it will print * and for each if condition fails it will print space  so  star will print  2 time .

*               *

Iteration-5

r=5 (passes the first for loop condition) because it will execute until r<= row.
second for loop will iterate up to  1 to row time and for each iteration it will check the condition if(r == 1 || r == row || c == 1 || c == row) for each if condition satisfy it will print * and for each if condition fails it will print space  so  star will print  5 time .

*  *  *  *  *

Now r=6, so first for loop condition will fail.  So next for loop will not be executed any more.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this

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

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
  		for(r = 1; r <= row ; r++)
        {
            for(c = 1; c <= row; c++)
                if(r == 1 || r == row || c == 1 || c == row)
                    printf("* "); 
                else
                    printf("  "); 
           	printf("\n"); 
        }	    
   return 0;
}
Output:

Enter rows : 5
*  *  *  *  *
*             *
*             *
*             *
*  *  *  *  *

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
   		for(r = 1; r <= row ; r++)
        {
            for(c = 1; c <= row; c++)
                if(r == 1 || r == row || c == 1 || c == row)
                    cout << "* "; 
                else
                    cout << "  "; 
           	cout <<"\n"; 
        }	    
    return 0;
    
}
Output:

Enter rows : 5
*  *  *  *  *
*             *
*             *
*             *
*  *  *  *  *

Related Java Star Pattern Programs: