Java Program to Print Solid Square Inside a Square Character Pattern

Printing Solid Square Inside a Square Character Pattern

In the previous article, we have discussed Java Program to Print Hollow Lower Triangular Pattern

In this article we are going to see how to print Solid Square Inside a square character pattern.

Example-1

When row value =10

ABCDEFGHIJ
A                 J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A                 J
ABCDEFGHIJ
Example-2

When row value = 5

ABCDE
A       E
A  C   E
A       E
ABCDE

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach:

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

Java Code to Print Solid Square Inside a 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;
    // Starting ASCII value taken 64 
    int asciiAlpha = 64;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //outer for loop to iterate all the rows
    for (r = 1; r <= row; r++)
        {
            // inner for loop to print column values for each row
            // means iterating the columns
            for ( c = 1; c <= row; c++)
            {
                // condition for printing stars
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                     System.out.print((char)(c + asciiAlpha));
                 else
                 // printing space
                    System.out.print(" ");
            }
            System.out.println();
        }
  }
}
Output :

Enter  rows: 10

ABCDEFGHIJ
A                 J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A                J
ABCDEFGHIJ

C Code to Print Solid Square Inside a Square Character Pattern

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

Enter  rows: 10

ABCDEFGHIJ
A                 J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A                 J
ABCDEFGHIJ

C++ Code to Print Solid Square Inside a Square Character Pattern

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

Enter  rows: 10

ABCDEFGHIJ
A                 J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A CDEFGH  J
A                 J
ABCDEFGHIJ

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:

Leave a Comment