Java Program to Print Hollow Square Character Pattern

Program to Print Hollow Square character Pattern

In the previous article, we have discussed Java Program to Print Asterisk Character Pattern

In this article we are going to see how to print the  hollow square character pattern.

Example-1

When number of  rows: 5

A B C D 
A        D    
A        D   
A        D  
A B C D
Example-1

When number of  rows: 5

A B C D E 
A           E 
A           E 
A           E 
A B C D E

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

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

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 characters according to the iteration  condition i.e.
    if(r == 1 || r == row || c == 1 || c == row) .

Java Code to Print Hollow Square Character Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    // starting ASCII value taken 64
    int ascii=64;
    //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 characters by checking condition 
            for(c = 1; c <= row; c++)
                if(r == 1 || r == row || c == 1 || c == row)
                    System.out.print((char)(c+ascii)+" "); 
                else
                    System.out.print("  "); 
            System.out.print("\n"); 
        }                
    }
}
Output:

Enter  rows: 5

A B C D E 
A           E 
A           E 
A           E 
A B C D E

C Code to Print Hollow Square Character Pattern

#include <stdio.h>
int main() {
   int r, row, c ,d;
   int ascii=64;
   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("%c ",(c+ascii)); 
                else
                    printf("  "); 
               printf("\n"); 
        }        
   return 0;
}
Output:

Enter  rows: 5

A B C D E 
A           E 
A           E 
A           E 
A B C D E

C++ Code to Print Hollow Square Character Pattern

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   // starting ASCII value taken 64
    int ascii=64;
   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 << (char)(c+ascii) << " "; 
                else
                    cout << "  "; 
               cout <<"\n"; 
        }        
    return 0;
    
}
Output: 
Enter rows: 5 
A B C D E 
A           E 
A           E 
A           E 
A B C D E

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: