Program to print Square inside a Square
Last Updated :
28 Feb, 2023
Given a number N, print a hollow square of side with N asterisks(‘*’), and inside it print a hollow square of side N-4 asterisks(‘*’).
Examples :
Input : 6
Output :
******
* *
* ** *
* ** *
* *
******
Input :9
Output :
*********
* *
* ***** *
* * * *
* * * *
* * * *
* ***** *
* *
*********
The idea is to run two nested loops from 1 to n for rows and columns and now to check for conditions when to print an asterisk(‘*’) and when to print space(‘ ‘). The condition for printing asterisks will be:
// This will print asterisks on the boundary
(i == 1 || i == n || j == 1 || j == n)
// This will print the asterisks on the boundary
// of inner square
(i >= 3 && i = 3 && j <= n - 2) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2))
All of the indexes which do not satisfy the above condition will contain space.
Below is the implementation of above idea:
C++
#include <iostream>
using namespace std;
void printPattern( int n)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if ((i == 1 || i == n || j == 1 || j == n) ||
(i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2))
{
cout<< "*" ;
}
else
{
cout<< " " ;
}
}
cout<<endl;
}
}
int main()
{
int n = 7;
printPattern(n);
return 0;
}
|
C
#include <stdio.h>
void printPattern( int n)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if ((i == 1 || i == n || j == 1 || j == n) ||
(i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2))
{
printf ( "*" );
}
else
{
printf ( " " );
}
}
printf ( "\n" );
}
}
int main()
{
int n = 7;
printPattern(n);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static void printPattern( int n) {
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= n; j++) {
/*** For printing the required square pattern ***/
if ((i == 1 || i == n || j == 1 || j == n) ||
(i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2 ) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2 ))
{
System.out.print( "*" );
}
else
{
System.out.print( " " );
}
}
System.out.print( "\n" );
}
}
public static void main(String[] args) {
int n = 7 ;
printPattern(n);
}
}
|
Python3
def printPattern(n):
for i in range ( 1 ,n + 1 ):
for j in range ( 1 ,n + 1 ):
if ((i = = 1 or i = = n or j = = 1 or j = = n) or
(i > = 3 and i < = n - 2 and j > = 3 and j < = n - 2 ) and
(i = = 3 or i = = n - 2 or j = = 3 or j = = n - 2 )):
print ( "*" ,end = "")
else :
print ( " " ,end = " ") # Prints space(" ")
print ()
n = 7
printPattern(n)
|
C#
using System;
class GFG {
static void printPattern( int n) {
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n; j++) {
if ((i == 1 || i == n || j == 1
|| j == n) || (i >= 3 &&
i <= n - 2 && j >= 3 &&
j <= n - 2) && (i == 3 ||
i == n - 2 || j == 3 ||
j == n - 2))
{
Console.Write( "*" );
}
else
{
Console.Write( " " );
}
}
Console.WriteLine();
}
}
public static void Main() {
int n = 7;
printPattern(n);
}
}
|
PHP
<?php
function printPattern( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $n ; $j ++)
{
if (( $i == 1 || $i == $n ||
$j == 1 || $j == $n ) ||
( $i >= 3 && $i <= $n - 2 &&
$j >= 3 && $j <= $n - 2) &&
( $i == 3 || $i == $n - 2 ||
$j == 3 || $j == $n - 2))
{
echo "*" ;
}
else
{
echo " " ;
}
}
echo "\n" ;
}
}
$n = 7;
printPattern( $n );
?>
|
Javascript
function printPattern(n) {
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= n; j++) {
if ((i == 1 || i == n || j == 1 || j == n) ||
(i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2) &&
(i == 3 || i == n - 2 || j == 3 || j == n - 2))
{
process.stdout.write( "*" );
}
else
{
process.stdout.write( " " );
}
}
process.stdout.write( "\n" );
}
}
let n = 7;
printPattern(n);
|
Output :
*******
* *
* *** *
* * * *
* *** *
* *
*******
Time Complexity: O(n2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...