Program to Print Hollow Square Inside a Square Character Pattern
In the previous article, we have discussed Java Program to Print Solid Square Inside a Square Character Pattern
In this article we are going to see how to print Hollow Square Inside a square character pattern.
- Java Code to Print Hollow Square Inside a Square Character Pattern
- C Code to Print Hollow Square Inside a Square Character Pattern
- C++ Code to Print Hollow Square Inside a Square Character Pattern
Example: When row value = 10 ABCDEFGHIJ A J A CDEFGH J A C H J A C H J A C H J A C H J A CDEFGH J A J ABCDEFGHIJ
Now, let’s see the actual program printing it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print the row value and 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 &&
else it will print spaces .
c <= row - 2) && (r == 3 || r == row - 2 || c == 3 || c == row - 2)) - Then go on printing the character according to loop.
Java Code to Print Hollow 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 of Scanner class 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 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)) //printing character System.out.print((char)(c + asciiAlpha)); else //printing space System.out.print(" "); } System.out.print("\n"); } } }
Output : Enter rows : 10 ABCDEFGHIJ A J A CDEFGH J A C H J A C H J A C H J A C H J A CDEFGH J A J ABCDEFGHIJ
C Code to Print Hollow 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) && (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 C H J A C H J A C H J A C H J A CDEFGH J A J ABCDEFGHIJ
C++ Code to Print Hollow 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) && (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 C H J A C H J A C H J A C H 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: