Hollow rectangle star pattern :
The task is print below hollow pattern of given dimension.
********************
* *
* *
* *
* *
********************
- Input number of rows and columns.
- For rows of rectangle run the outer loop from 1 to rows.
for (i = 1; i < = rows; i++)
- For column of rectangle run the inner loop from 1 to columns.
for (j = 1; j < = columns; j++)
- Print star for first or last row or for first or last column, otherwise print blank space.
- After printing all columns of a row, print new line after inner loop.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void print_rectangle( int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i == 1 || i == n ||
j == 1 || j == m)
cout << "*" ;
else
cout << " " ;
}
cout << endl;
}
}
int main()
{
int rows = 6, columns = 20;
print_rectangle(rows, columns);
return 0;
}
|
C
#include <stdio.h>
void print_rectangle( int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i==1 || i==n || j==1 || j==m)
printf ( "*" );
else
printf ( " " );
}
printf ( "\n" );
}
}
int main()
{
int rows = 6, columns = 20;
print_rectangle(rows, columns);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void print_rectangle( int n, int m)
{
int i, j;
for (i = 1 ; i <= n; i++)
{
for (j = 1 ; j <= m; j++)
{
if (i == 1 || i == n ||
j == 1 || j == m)
System.out.print( "*" );
else
System.out.print( " " );
}
System.out.println();
}
}
public static void main(String args[])
{
int rows = 6 , columns = 20 ;
print_rectangle(rows, columns);
}
}
|
Python3
def print_rectangle(n, m) :
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , m + 1 ) :
if (i = = 1 or i = = n or
j = = 1 or j = = m) :
print ( "*" , end = "")
else :
print ( " " , end = "")
print ()
rows = 6
columns = 20
print_rectangle(rows, columns)
|
C#
using System;
public class GFG
{
static void print_rectangle( int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i == 1 || i == n ||
j == 1 || j == m)
Console.Write( "*" );
else
Console.Write( " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int rows = 6, columns = 20;
print_rectangle(rows, columns);
}
}
|
PHP
<?php
function print_rectangle( $n , $m )
{
$i ;
$j ;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $m ; $j ++)
{
if ( $i == 1 || $i == $n ||
$j == 1 || $j == $m )
echo ( "*" );
else
echo ( " " );
}
echo ( "\n" );
}
}
$rows = 6;
$columns = 20;
print_rectangle( $rows , $columns );
?>
|
Javascript
<script>
function print_rectangle(n, m)
{
var i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i == 1 || i == n || j == 1 || j == m)
document.write( "*" );
else
document.write( " " );
}
document.write( "<br>" );
}
}
var rows = 6,
columns = 20;
print_rectangle(rows, columns);
</script>
|
Output
********************
* *
* *
* *
* *
********************
Time Complexity: O(m * n), where m and n represents the given inputs.
Auxiliary Space: O(1), no extra space is required, so it is a constant.