Python Program to Print Hollow Box Pattern of Numbers

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows and number of columns of the box, the task is to print the Hollow Box Number pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the box =11
given number of columns of the box =19

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Example2:

Input:

given number of rows of the box =11
given number of columns of the box =19
given character to print ='&'

Output:

& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Program to Print Hollow Box Number Pattern in C, C++, and Python

Below are the ways to Print Hollow Box Number Pattern in C, C++, and python.

Method #1: Using For Loop (Static Character)

Approach:

  • Give the number of rows and number of columns of the box as static or user input and store them in two separate variables.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print 1 with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns of the box as static or user input
# and store them in two separate variables.
boxrows = 11
boxcolumns = 19
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print 1 with a space character.
            print("1", end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                cout << "1 ";
            }
            else {
                //  Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                printf("1 ");
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows and number of columns of the box as user input and store them in two separate variables.
  • Give the character to print as a box pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print the given character with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the box as user input using int(input()) and store it in a variable.
  • Give the number of columns of the box as user input using int(input()) and store it in another variable.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows and number of columns of the box as user input and store them in two separate variables.
boxrows = int(input("Enter some random number of rows of the box = "))
boxcolumns = int(input("Enter some random number of columns of the box = "))
# Give the character to print as a box pattern as user input and store it in a variable.
userchar = input('Enter some random character to print = ')
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print the given character with a space character.
            print(userchar, end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

2) C++ Implementation

  • Give the number of rows of the box as user input using cin and store it in a variable.
  • Give the number of columns of the box as user input using cin and store it in another variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the box as user input
    // using cin and store it in a variable.
    // Give the number of columns of the box as user input
    // using cin and store it in another variable.
    int boxrows;
    int boxcolumns;
    cin >> boxrows;
    cin >> boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    cin >> userchar;
    cout << endl;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                cout << userchar << " ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

3) C Implementation

  • Give the number of rows of the box as user input using scanf and store it in a variable.
  • Give the number of columns of the box as user input using scanf and store it in another variable.
  • Give the Character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the box as user input
    // using scanf and store it in a variable.
    // Give the number of columns of the box as user input
    // using scanf and store it in another variable.
    int boxrows;
    int boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    scanf("%d%d%c",&boxrows,&boxcolumns,&userchar);
    printf("\n");
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                printf("%c ", userchar);
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Related Programs: