Program to print solid and hollow square patterns
Last Updated :
13 Mar, 2023
For any given number n, print Hollow and solid Squares and Rhombus made with stars(*).
Examples:
Input : n = 4
Output :
Solid Square:
****
****
****
****
Hollow Square:
****
* *
* *
****
1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both. Where the outer loop is used for numbers of rows and the inner loop is used for printing all stars in a particular row.
2. Hollow Square : Hollow Square requires a little bit improvisation. Here we should again use two loops, where 1st and last row must have all n-stars and remaining rows have stars only in 1st and last column.
C++
#include <bits/stdc++.h>
using namespace std;
void hollowSquare( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
if (i==1 || i==rows)
for (j=1; j<=rows; j++)
cout << "*" ;
else
for (j=1; j<=rows; j++)
if (j==1 || j==rows)
cout << "*" ;
else
cout << " " ;
cout << "\n" ;
}
}
void solidSquare( int rows)
{
int i, j;
for (i=1; i<=rows; i++)
{
for (j=1; j<=rows; j++)
cout << "*" ;
cout << "\n" ;
}
}
void printPattern( int rows)
{
cout << "\nSolid Square:\n" ;
solidSquare(rows);
cout << "\nHollow Square:\n" ;
hollowSquare(rows);
}
int main()
{
int rows = 5;
printPattern (rows);
return 0;
}
|
Java
class GFG
{
static void hollowSquare( int rows)
{
int i, j;
for (i = 1 ; i <= rows; i++)
{
if (i == 1 || i == rows)
for (j = 1 ; j <= rows; j++)
System.out.print( "*" );
else
for (j = 1 ; j <= rows; j++)
if (j == 1 || j == rows)
System.out.print( "*" );
else
System.out.print( " " );
System.out.print( "\n" );
}
}
static void solidSquare( int rows)
{
int i, j;
for (i = 1 ; i <= rows; i++)
{
for (j = 1 ; j <= rows; j++)
System.out.print( "*" );
System.out.print( "\n" );
}
}
static void printPattern( int rows)
{
System.out.print( "\nSolid Square:\n" );
solidSquare(rows);
System.out.print( "\nHollow Square:\n" );
hollowSquare(rows);
}
public static void main (String[] args)
{
int rows = 5 ;
printPattern (rows);
}
}
|
Python3
def hollowSquare(rows):
for i in range ( 1 , rows + 1 ):
if (i = = 1 or i = = rows):
for j in range ( 1 , rows + 1 ):
print ( "*" , end = "")
else :
for j in range ( 1 , rows + 1 ):
if (j = = 1 or j = = rows):
print ( "*" , end = "")
else :
print (end = " " )
print ()
def solidSquare(rows):
for i in range ( 1 , rows):
for j in range ( 1 , rows + 1 ):
print ( "*" , end = "")
print ()
def printPattern(rows):
print ( "Solid Square:" )
solidSquare(rows)
print ( "\nHollow Square:" )
hollowSquare(rows)
rows = 5
printPattern (rows)
|
C#
using System;
class GFG
{
static void hollowSquare( int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++)
Console.Write( "*" );
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows)
Console.Write( "*" );
else
Console.Write( " " );
Console.WriteLine();
}
}
static void solidSquare( int rows)
{
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
Console.Write( "*" );
Console.WriteLine();
}
}
static void printPattern( int rows)
{
Console.Write( "\nSolid Square:\n" );
solidSquare(rows);
Console.Write( "\nHollow Square:\n" );
hollowSquare(rows);
}
public static void Main ()
{
int rows = 5;
printPattern (rows);
}
}
|
PHP
<?php
function hollowSquare( $rows )
{
for ( $i = 1; $i <= $rows ; $i ++)
{
if ( $i == 1 || $i == $rows )
for ( $j = 1; $j <= $rows ; $j ++)
echo "*" ;
else
for ( $j = 1; $j <= $rows ; $j ++)
if ( $j == 1 || $j == $rows )
echo "*" ;
else
echo " " ;
echo "\n" ;
}
}
function solidSquare( $rows )
{
for ( $i = 1; $i <= $rows ; $i ++)
{
for ( $j = 1; $j <= $rows ; $j ++)
echo "*" ;
echo "\n" ;
}
}
function printPattern( $rows )
{
echo "\nSolid Square:\n" ;
solidSquare( $rows );
echo "\nHollow Square:\n" ;
hollowSquare( $rows );
}
$rows = 5;
printPattern ( $rows );
?>
|
Javascript
<script>
function hollowSquare(rows) {
var i, j;
for (i = 1; i <= rows; i++) {
if (i == 1 || i == rows)
for (j = 1; j <= rows; j++) document.write( "*" );
else
for (j = 1; j <= rows; j++)
if (j == 1 || j == rows) document.write( "*" );
else document.write( " " );
document.write( "<br>" );
}
}
function solidSquare(rows) {
var i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++) document.write( "*" );
document.write( "<br>" );
}
}
function printPattern(rows) {
document.write( "Solid Square:<br>" );
solidSquare(rows);
document.write( "<br>Hollow Square:<br>" );
hollowSquare(rows);
}
var rows = 5;
printPattern(rows);
</script>
|
Output :
Solid Square:
*****
*****
*****
*****
*****
Hollow Square:
*****
* *
* *
* *
*****
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...