Print rectangle pattern
In this article, we will discuss how to write a C program to print a rectangle pattern using asterisks (*) as the border and empty spaces for the interior. The program will take the height and width of the rectangle as inputs and display the corresponding pattern.
Problem Statement
We want to write a program that can generate a rectangle pattern using asterisks. The program should take the height and width of the rectangle as input and print the pattern on the console.
For example, if the height is 5 and the width is 5, the program should output:
* * * * * * * * * * * * * * * *
Algorithm
1. Start the program.
2. Define a function named draw_rectangle
that takes the height and width of the rectangle as parameters.
3. Check if the height and width are less than or equal to 2. If so, return from the function.
4. Print the height and width of the rectangle.
5. Use two nested loops to iterate through each row and column of the rectangle.
6. Inside the loops, check if the current position is on the border of the rectangle (first row, last row, first column, or last column). If so, print an asterisk (*).
7. If the current position is not on the border, print two spaces to create the interior of the rectangle.
8. After the inner loop, print a newline character to move to the next row.
9. Return from the function.
10. In the main
function, call the draw_rectangle
function with different test cases.
11. End the program.
Pseudocode
//This function is used to draw a rectangle of a given width and height.
void draw_rectangle(int height, int width)
{
if (height <= 2 || width <= 2)
{
//When height and width are less than three
return;
}
print("Height : ", height, " Width : ", width, "\n\n");
for (i = 0; i < height; ++i)
{
for (j = 0; j < width; ++j)
{
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
print("* ");
}
else
{
print(" ");
}
}
print("\n");
}
}
Explanation
The given program uses a function called draw_rectangle
to draw a rectangle pattern. The function takes the height and width of the rectangle as inputs.
First, the function checks if the height and width are less than or equal to 2. If they are, it means the rectangle cannot be formed, so the function returns without doing anything.
If the height and width are greater than 2, the function proceeds to print the height and width of the rectangle. Then, using two nested loops, it iterates through each position in the rectangle.
For each position, the function checks if it is on the border of the rectangle. If it is, it prints an asterisk (*). Otherwise, it prints two spaces to create the interior of the rectangle.
After printing each row, the function moves to the next line using the newline character ("\n"). Finally, the function returns.
In the main
function, the draw_rectangle
function is called with different test cases to demonstrate the pattern for rectangles of various sizes.
Code Solution
Here given code implementation process.
//C Program
//Display rectangle pattern
#include <stdio.h>
//This function is used to draw a rectangle of a given width and height.
void draw_rectangle(int height, int width)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
printf("\nHeight : %d Width : %d\n\n", height, width);
int i = 0, j = 0;
//This Loop controlling the height of rectangle
for (i = 0; i < height; ++i)
{
//This Loop controlling the width of rectangle
for (j = 0; j < width; ++j)
{
//This condition is display outer layer of rectangle
//When anyone is a valid
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
printf("* ");
}
else
{
//include 2 spaces
printf(" ");
}
}
//include new line
printf("\n");
}
}
int main()
{
//Test Cases
draw_rectangle(5, 5);
draw_rectangle(7, 9);
draw_rectangle(3, 7);
return 0;
}
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
Java Program
Display rectangle pattern
*/
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
public void draw_rectangle(int height, int width)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
System.out.print("\nHeight : " + height + " Width : " + width + "\n\n");
int i = 0, j = 0;
//This Loop controlling the height of rectangle
for (i = 0; i < height; ++i)
{
//This Loop controlling the width of rectangle
for (j = 0; j < width; ++j)
{
//This condition is display outer layer of rectangle
//When anyone is a valid
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
System.out.print("* ");
}
else
{
//include 2 spaces
System.out.print(" ");
}
}
//include new line
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
}
}
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
C++ Program
Display rectangle pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public:
//This function is used to draw a rectangle of a given width and height.
void draw_rectangle(int height, int width)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
cout << "\nHeight : " << height << " Width : " << width << "\n\n";
int i = 0, j = 0;
//This Loop controlling the height of rectangle
for (i = 0; i < height; ++i)
{
//This Loop controlling the width of rectangle
for (j = 0; j < width; ++j)
{
//This condition is display outer layer of rectangle
//When anyone is a valid
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
cout << "* ";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj ;
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
return 0;
}
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
C# Program
Display rectangle pattern
*/
using System;
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
public void draw_rectangle(int height, int width)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
Console.Write("\nHeight : " + height + " Width : " + width + "\n\n");
int i = 0, j = 0;
//This Loop controlling the height of rectangle
for (i = 0; i < height; i++)
{
//This Loop controlling the width of rectangle
for (j = 0; j < width; j++)
{
//When anyone is a valid
//This condition is display outer layer of rectangle
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
Console.Write("* ");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
}
}
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
<?php
/*
Php Program
Display rectangle pattern
*/
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
function draw_rectangle($height, $width)
{
if ($height <= 2 || $width <= 2)
{
//When height and width less than three
return;
}
echo "\nHeight : ". $height ." Width : ". $width ."\n\n";
$i = 0;
$j = 0;
//This Loop controlling the height of rectangle
for ($i = 0; $i < $height; ++$i)
{
//This Loop controlling the width of rectangle
for ($j = 0; $j < $width; ++$j)
{
//When anyone is a valid
//This condition is display outer layer of rectangle
if ($i == 0 || $i == $height - 1 || $j == 0 || $j == $width - 1)
{
echo "* ";
}
else
{
echo " ";
}
}
echo "\n";
}
}
}
function main()
{
$obj = new MyPattern();
//Test Cases
$obj->draw_rectangle(5, 5);
$obj->draw_rectangle(7, 9);
$obj->draw_rectangle(3, 7);
}
main();
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
Node Js Program
Display rectangle pattern
*/
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
draw_rectangle(height, width)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
process.stdout.write("\nHeight : " + height + " Width : " + width + "\n\n");
var i = 0;
var j = 0;
//This Loop controlling the height of rectangle
for (i = 0; i < height; ++i)
{
//This Loop controlling the width of rectangle
for (j = 0; j < width; ++j)
{
//When anyone is a valid
//This condition is display outer layer of rectangle
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
process.stdout.write("* ");
}
else
{
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}
function main()
{
var obj = new MyPattern();
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
}
main();
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
# Python 3 Program
# Display rectangle pattern
class MyPattern :
# This function is used to draw a rectangle of a given width and height.
def draw_rectangle(self, height, width) :
if (height <= 2 or width <= 2) :
# When height and width less than three
return
print("\nHeight : ", height ," Width : ", width ,"\n\n", end = "")
i = 0
j = 0
# This Loop controlling the height of rectangle
i = 0
while (i < height) :
# This Loop controlling the width of rectangle
j = 0
while (j < width) :
# When anyone is a valid
# This condition is display outer layer of rectangle
if (i == 0 or i == height - 1 or j == 0 or j == width - 1) :
print("* ", end = "")
else :
print(" ", end = "")
j += 1
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Test Cases
obj.draw_rectangle(5, 5)
obj.draw_rectangle(7, 9)
obj.draw_rectangle(3, 7)
if __name__ == "__main__": main()
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
# Ruby Program
# Display rectangle pattern
class MyPattern
# This function is used to draw a rectangle of a given width and height.
def draw_rectangle(height, width)
if (height <= 2 || width <= 2)
# When height and width less than three
return
end
print("\nHeight : ", height ," Width : ", width ,"\n\n")
i = 0
j = 0
# This Loop controlling the height of rectangle
i = 0
while (i < height)
# This Loop controlling the width of rectangle
j = 0
while (j < width)
# When anyone is a valid
# This condition is display outer layer of rectangle
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
print("* ")
else
# include 2 spaces
print(" ")
end
j += 1
end
# include new line
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Test Cases
obj.draw_rectangle(5, 5)
obj.draw_rectangle(7, 9)
obj.draw_rectangle(3, 7)
end
main()
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
Scala Program
Display rectangle pattern
*/
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
def draw_rectangle(height: Int, width: Int): Unit = {
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
print("\nHeight : " + height + " Width : " + width + "\n\n");
var i: Int = 0;
var j: Int = 0;
//This Loop controlling the height of rectangle
i = 0;
while (i < height)
{
//This Loop controlling the width of rectangle
j = 0;
while (j < width)
{
//When anyone is a valid
//This condition is display outer layer of rectangle
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
print("* ");
}
else
{
//include 2 spaces
print(" ");
}
j += 1;
}
//include new line
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
}
}
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
/*
Swift Program
Display rectangle pattern
*/
class MyPattern
{
//This function is used to draw a rectangle of a given width and height.
func draw_rectangle(_ height: Int, _ width: Int)
{
if (height <= 2 || width <= 2)
{
//When height and width less than three
return;
}
print("\nHeight : ", height ," Width : ", width ,"\n\n", terminator: "");
var i: Int = 0;
var j: Int = 0;
//This Loop controlling the height of rectangle
i = 0;
while (i < height)
{
//This Loop controlling the width of rectangle
j = 0;
while (j < width)
{
//This condition is display outer layer of rectangle
//When anyone is a valid
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
print("* ", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Cases
obj.draw_rectangle(5, 5);
obj.draw_rectangle(7, 9);
obj.draw_rectangle(3, 7);
}
main();
Output
Height : 5 Width : 5
* * * * *
* *
* *
* *
* * * * *
Height : 7 Width : 9
* * * * * * * * *
* *
* *
* *
* *
* *
* * * * * * * * *
Height : 3 Width : 7
* * * * * * *
* *
* * * * * * *
Resultant Output Explanation
For the test case where the height is 5 and the width is 5, the program outputs a rectangle pattern with a border of asterisks and an empty interior. The outermost layer of the rectangle is filled with asterisks, while the interior is empty (represented by spaces).
The same pattern is followed for the other test cases as well, where the height and width are different. The border of the rectangle is always filled with asterisks, and the interior remains empty.
The program successfully generates the desired rectangle pattern for different input values of height and width.
Time Complexity
The time complexity of the program is O(height * width) because there are two nested loops that iterate through each position in the rectangle. The number of iterations depends on the height and width of the rectangle.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment