Python Program to Print Box Number Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

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

Examples:

Outer Elements 1 and inner Elements 0:

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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Outer Elements 0 and inner Elements 1:

Example2:

Input:

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

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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

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

Method #1: Box Pattern with 1 and 0 characters (Outer Elements 1 and Inner elements 0)

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 0 with a 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 0 with a space character.
            print("0", 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 with a space character.
                cout << "0 ";
            }
        }
        // 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 with a space character.
                printf("0 ");
            }
        }
        // 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Method #2: Box Pattern with 1 and 0 characters (Outer Elements 0 and Inner elements 1)

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 0 with a space character.
  • Else print 1 with a 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 0 with a space character.
            print("0", end=" ")
        else:
            # Else print 1 with a space character.
            print("1", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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 0
                // with a space character.
                cout << "0 ";
            }
            else {
                // Else print 1 with a space character.
                cout << "1 ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

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 0
                // with a space character.
                printf("0 ");
            }
            else {
                // Else print 1 with a space character.
                printf("1 ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Related Programs: