Python Program to Print Hollow Square Star Pattern

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given the sides of the square, the task is to print the hollow Square pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of sides of square =6

Output:

* * * * * * 
*           * 
*           * 
*           * 
*           * 
* * * * * *

Example2:

Input:

given number of sides of square =8
given character to print =+

Output:

+ + + + + + + + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+ + + + + + + +

Program to Print Hollow Square Star Pattern in C, C++, and Python

Below are the ways to print the hollow square star pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the side of the square as static input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the side of the square as static input and store it in a variable.
squareside = 6
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

* * * * * * 
*           * 
*           * 
*           * 
*           * 
* * * * * *

2) C++Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 19;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            // We use the If Else statement to check If the
            // side length is 0 or maximum – 1.
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

* * * * * * * * * * * * * * * * * * * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
* * * * * * * * * * * * * * * * * * *

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 5;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            // We use the If Else statement to check If the
            // side length is 0 or maximum – 1.
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

* * * * * 
*         * 
*         * 
*         * 
* * * * *

Method #2: Using For Loop (User Character)

Approach:

  • Give the side of the square as user input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1.
  • If it is true then print the given character else print space.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

# Give the side of the square as user input and store it in a variable.
squareside = int(input('Enter some random number of sides of square = '))
# Scan the character to print as user input and store it in a variable.
characte = input('Enter some random character to print = ')
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print the given character else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random number of sides of square = 7
Enter some random character to print = (
( ( ( ( ( ( ( 
(           ( 
(           ( 
(           ( 
(           ( 
(           ( 
( ( ( ( ( ( (

2) C++Implementation

  • Give the number of sides of the square as user input using cin and store it in a variable.
  • Create a character 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(void)
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of sides of the "
            "square = "
         << endl;
    cin >> sidesnum;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;

    // Using Nested For loops print the square pattern.
    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print the given character
            // else print space.
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1)
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of sides of the square = 
8
Enter some random character to print = 
+
+ + + + + + + + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+ + + + + + + +

3) C Implementation

  • Give the number of sides of the square as user input using scanf and store it in a variable.
  • Create a character variable.
  • Give the character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the square pattern.
    scanf("%d%c", &sidesnum, &characte);
    printf("\n");

    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print the given character
            // else print space.
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1)
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

8^
^ ^ ^ ^ ^ ^ ^ ^
^                      ^ 
^                      ^
^                      ^
^                      ^
^                      ^
^                      ^
^ ^ ^ ^ ^ ^ ^ ^

Related Programs: