Print Hollow Square Inside a Square Number Pattern
In the previous article, we have discussed Java Program to Print Hexagonal Number Pattern
In this article we are going to see how to print Hollow Square Inside a square number program.
- Java Code to Print Hollow Square Inside a Square Number Pattern
- C Code to Print Hollow Square Inside a Square Number Pattern
- C++ Code to Print Hollow Square Inside a Square Number Pattern
When number of rows:8 11111111 2 2 3 3333 3 4 4 4 4 5 5 5 5 6 6666 6 7 7 88888888
Now, let’s see the actual program printing it.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach:
- Enter total row and store it in an integer variable
row
. - Take first for loop to print the row value and number for each row.
- Take first inner for loop to print the column value i.e., numbers 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 numbers according to loop.
Java Code to Print Hollow Square Inside a Square Number Pattern
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(r); else System.out.print(" "); } System.out.print("\n"); } } }
Output: Enter rows : 8 11111111 2 2 3 3333 3 4 4 4 4 5 5 5 5 6 6666 6 7 7 88888888
C Code to Print Hollow Square Inside a Square Number Pattern
#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("%d",r); else printf(" "); } printf("\n"); } return 0; }
Output: Enter rows : 11111111 2 2 3 3333 3 4 4 4 4 5 5 5 5 6 6666 6 7 7 88888888
C++ Code to Print Hollow Square Inside a Square Number Pattern
#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 << r; else cout << " "; } cout << "\n"; } return 0; }
Output: Enter rows : 8 11111111 2 2 3 3333 3 4 4 4 4 5 5 5 5 6 6666 6 7 7 88888888
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 Number Pattern Programs: