Print Hollow Rectangle Pattern in C / C++ / Java / Python / C#

Hollow rectangle pattern

Programs to print the hollow rectangle pattern have been given here.






1. Program & output to print the hollow rectangle pattern




1.1. C Program & output to print the hollow rectangle pattern

Code has been copied
/*******************************************
      	     alphabetacoder.com
C program to print hollow rectangle pattern
********************************************/

#include <stdio.h>

int main() {
    // declare variables
    int l, w, i, j;

    // take input
    printf("Enter the length: ");
    scanf("%d", & l);
    printf("Enter the width: ");
    scanf("%d", & w);

    // new line
    printf("\n");

    // display the pattern
    for (i = 1; i <= w; i++) {
        for (j = 1; j <= l; j++) {
            // for the first and last row
            if (i == 1 || i == w) {
                printf("*");
            }
            // except the first and last row
            else {
                // for the first and last column
                if (j == 1 || j == l) {
                    printf("*");
                }
                // for other columns
                else {
                    printf(" ");
                }
            }
        }
        printf("\n");
    }

    return 0;
}

Output


Enter the length: 8

Enter the width: 6


********

*      *

*      *

*      *

*      *

********




1.2. C++ Program & output to print the hollow rectangle pattern

Code has been copied
/*********************************************
      	     alphabetacoder.com
C++ program to print hollow rectangle pattern
**********************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int l, w, i, j;

    // take input
    cout << "Enter the length: ";
    cin >> l;
    cout << "Enter the width: ";
    cin >> w;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= w; i++) {
        for (j = 1; j <= l; j++) {
            // for the first and last row
            if (i == 1 || i == w) {
                cout << "*";
            }
            // except the first and last row
            else {
                // for the first and last column
                if (j == 1 || j == l) {
                    cout << "*";
                }
                // for other columns
                else {
                    cout << " ";
                }
            }
        }
        cout << "\n";
    }

    return 0;
}

Output


Enter the length: 10

Enter the width: 6


**********

*        *

*        *

*        *

*        *

**********




1.3. Java Program & output to print the hollow rectangle pattern

Code has been copied
/***********************************************
      	     alphabetacoder.com
Java program to print hollow rectangle pattern
************************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
	// declare instance of Scanner class
        Scanner sc = new Scanner(System.in);		
	// declare variables
	int l, w, i, j;

	// take input
	System.out.print("Enter the length: ");
	l = sc.nextInt();
	System.out.print("Enter the width: ");
	w = sc.nextInt();

	// new line
	System.out.print("\n");

	// display the pattern
	for (i = 1; i <= w; i++) {
	    for (j = 1; j <= l; j++) {
		// for the first and last row
		if (i == 1 || i == w) {
		    System.out.print("*");
		}
		// except the first and last row
		else {
		    // for the first and last column
		    if (j == 1 || j == l) {
			System.out.print("*");
		    }
		    // for other columns
		    else {
			System.out.print(" ");
		    }
		}
	   }
	   System.out.print("\n");
	}
    }
}

Output


Enter the length: 6

Enter the width: 3


******

*    *

******




1.4. Python Program & output to print the hollow rectangle pattern

Code has been copied
#************************************************
#      	     alphabetacoder.com
#Python program to print hollow rectangle pattern
#*************************************************

# take input
l = int(input("Enter the length: "))
w = int(input("Enter the width: "))

# new line
print("")

# display the pattern
for i in range(1, w + 1):
    for j in range(1, l + 1):
        # for the first and last row
        if i == 1 or i == w:
            print("*", end = "")
        # except the first and last row
        else:
            # for the first and last column
            if j == 1 or j == l:
                print("*", end = "")
            # for other columns
            else:
                print(" ", end = "")
    # new line
    print("")

Output


Enter the length: 7

Enter the width: 4


*******

*     *

*     *

*******




1.5. C# Program & output to print the hollow rectangle pattern

Code has been copied
/************************
  alphabetacoder.com
C# program to to print 
hollow rectangle pattern
*************************/

using System;

namespace HollowRectangle {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int l, w, i, j;

            // take input
            Console.Write("Enter the length: ");
            l = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the width: ");
            w = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 1; i <= w; i++) {
                for (j = 1; j <= l; j++) {
                    // for the first and last row
                    if (i == 1 || i == w) {
                        Console.Write("*");
                    }
                    // except the first and last row
                    else {
                        // for the first and last column
                        if (j == 1 || j == l) {
                            Console.Write("*");
                        }
                        // for other columns
                        else {
                            Console.Write(" ");
                        }
                    }
                }
                Console.WriteLine("");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the length: 9

Enter the width: 5


*********

*       *

*       *

*       *

*********