Print rectangle pattern
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
* * * * * * *
* *
* * * * * * *
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