Java Program to Print Solid Square Inside a Square Number Pattern

Print Solid Square Inside a Square Number Pattern

In the previous article, we have discussed Java Program to Print Hollow Square Inside a Square Number Pattern

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

Output:

Enter rows : 8

11111111
2            2
3  3333  3
4  4444  4
5  5555  5
6  6666  6
7            7
88888888

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

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print the row value and a numbers 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 && c <= row - 2))  else it will print spaces .
  • Then go on printing the numbersl according to loop.

Java Code to Print Solid 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++)
        {
            for ( c = 1; c <= row; c++)
            {
                // condition for printing numbers
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                     System.out.print(r);
                 else
                    System.out.print(" ");
            }
            System.out.println();
        }
  }
}
Output:

Enter rows : 8

11111111
2            2
3  3333  3
4  4444  4
5  5555  5
6  6666  6
7            7
88888888

C Code to Print Solid 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))
                      printf("%d",r);
                 else
                    printf(" ");
            }
             printf("\n");
        }
   return 0;
}
Output:

Enter rows : 8

11111111
2            2
3  3333  3
4  4444  4
5  5555  5
6  6666  6
7            7
88888888

C++ Code to Print Solid 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))
                     cout << r;
                 else
                    cout << " ";
            }
            cout << "\n";
        }
   return 0;
}
Output:

Enter rows : 8

11111111
2            2
3  3333  3
4  4444  4
5  5555  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: