Java Program to Print Hollow Square with Repeated Number Pattern

Print Hollow Square with Repeated Number Pattern

In the previous article, we have discussed Java Program to Print Square with Spiral Number Pattern

In this program we are going to see how to print the  hollow square with repeated number pattern.

Example-1

When size value=5 and 
number = 9

9 9 9 9 9
9          9
9          9
9          9
9 9 9 9 9
Example-2:

When size value=3 and 
number = 5

5 5 5
5    5
5 5 5

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 size and number store them in integer variables size & num .
  • Take one outer for loop to iterate the rows,
  • Take one inner for loop to iterate the columns.
  • After each iteration print a new line.

Java Code to Print Hollow Square with Repeated Number Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
    //Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking size as input from user
    System.out.print("Size of square : ");
    int size = scan.nextInt();

    //Taking number as input from user
    System.out.print("Number to print : ");
    int num = scan.nextInt();

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 1 to the size entered by the user
        for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++)
        {
            //Prints the num value if condition matches else prints space
            if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size)
                System.out.print(num+ " ");
            else
                System.out.print("  ");
        }
        //Prints a newline
        System.out.println();
    }
}
}
Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
9          9 
9          9 
9          9 
9 9 9 9 9

C Code to Print Hollow Square with Repeated Number Pattern

#include <stdio.h>

int main()
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    scanf("%d", &size);

    //Taking number as input from user
    printf("Number to print : ");
    int num;
    scanf("%d", &num);

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 1 to the size entered by the user
        for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++)
        {
            //Prints the num value if condition matches else prints space
            if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size)
                printf("%d ", num);
            else
                printf("  ");
        }
        //Prints a newline
        printf("\n");
    }
    return 0;
}
Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
9          9 
9          9 
9          9 
9 9 9 9 9

C++ Code to Print Hollow Square with Repeated Number Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    cin >> size;

    //Taking number as input from user
    printf("Number to print : ");
    int num;
    cin >> num;

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 1; numberOfColumns <= size; numberOfColumns++)
        {
            //Prints the num value if condition matches else prints space
            if (numberOfColumns == 1 || numberOfColumns == size || numberOfRows == 1 || numberOfRows == size)
                cout << num << " ";
            else
                cout << "  ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
9          9 
9          9 
9          9 
9 9 9 9 9

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: