Java Program to Print Hollow Square Inside a Square Star Pattern

Program to Print Hollow Square Inside a Square Star Pattern

In this article we are going to see how to print Hollow Square Inside a square star program.

Example-1

When row value=10

**********
*            *
*  ****** *
*  *      * *
*  *      * *
*  *      * *
*  *      * *
*  ****** *
*            *
**********
Example-2:

When row value=7

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

Now, let’s see the actual program printing  it.

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print the row value and a star for each row.
  • Take first inner for loop to print the column value i.e., stars according to the condition
    if ((r == 1 || r == row  || c == 1 || c == row) || (r >= 3 && r <= row - 2 && c >= 3 &&
    c <= row - 2) && (r == 3 || r == row - 2 || c == 3 || c == row - 2))
      else it will print spaces .
  •    Then go on printing the star symbol according to loop.

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;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //outer for loop 
   for (r = 1; r <= row ; r++) 
   {
      // To print  columns of the square
        for (c = 1; c <=row ; c++) 
        {
        // For printing the square pattern 
             if ((r == 1 || r == row  || c == 1 || c == row) || (r >= 3 && r <= row - 2 && c >= 3 && c <= row-2) && (r == 3 || r == row - 2 || c == 3 || c == row - 2)) 
                 System.out.print("*"); 
             else
            System.out.print(" "); 
        }
         System.out.print("\n");
   }
  }
} 

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;
    //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 square = s.next().charAt(0);
    //outer for loop 
   for (r = 1; r <= row ; r++) 
   {
      // To print  columns of the square
        for (c = 1; c <=row ; c++) 
        {
        // For printing the square pattern 
             if ((r == 1 || r == row  || c == 1 || c == row) || (r >= 3 && r <= row - 2 && c >= 3 && c <= row-2) && (r == 3 || r == row - 2 || c == 3 || c == row - 2)) 
                 System.out.print(square); 
             else
            System.out.print(" "); 
        }
         System.out.print("\n");
   }
  }
} 

Output :

Enter row :  7
Enter character : *

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

C Code:

#include <stdio.h>
int main() 
{
   int r, row, c;
   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) || (r >= 3 && r <= row - 2 && c >= 3 && c <= row - 2)
 && (r == 3 || r == row - 2 || c == 3 || c == row - 2)) 
                  printf("*"); 
             else
             printf(" "); 
        }
          printf("\n");
   }
   return 0;
}
 

Output :

Enter row :  7

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ;
   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) || (r >= 3 && r <= row - 2 && c >= 3 && c <= row - 2) 
&& (r == 3 || r == row - 2 || c == 3 || c == row - 2)) 
                 cout << "*"; 
             else
            cout << " "; 
        }
         cout << "\n";
   }
   return 0;
}



Output :

Enter row :  7

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

Related Java Star Pattern Programs: