Friday, October 17, 2025
HomeData Modelling & AIProgram to print Square inside a Square

Program to print Square inside a Square

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++




// C++ program to print square inside
// a square pattern
#include <iostream>
using namespace std;
 
 
// Function to print the pattern square
// inside a square
void printPattern(int n)
{
    int i, j;
     
    // To access rows of the square
    for (i = 1; i <= n; i++)
    {
        // To access columns of the square
        for (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))
            {
                // Prints star(*)
                cout<<"*";
            }
            else
            {
                // Prints space(" ")
                cout<<" ";
            }
        }
     
        cout<<endl;
    }
}
 
// Driver Code
int main()
{
    int n = 7;
    printPattern(n);
    return 0;
}
 
// This code is contributed by Shivam.Pradhan.


C




// C program to print square inside
// a square pattern
#include <stdio.h>
 
// Function to print the pattern square
// inside a square
void printPattern(int n)
{
    int i, j;
     
    // To access rows of the square
    for (i = 1; i <= n; i++)
    {
        // To access columns of the square
        for (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))
            {
                // Prints star(*)
                printf("*");
            }
            else
            {
                // Prints space(" ")
                printf(" ");
            }
        }
     
        printf("\n");
    }
}
 
// Driver Code
int main()
{
    int n = 7;
    printPattern(n);
    return 0;
}


Java




// Java program to print square inside
// a square pattern
import java.lang.*;
 
class GFG {
     
// Function to print the pattern square
// inside a square
static void printPattern(int n) {
 
    // To access rows of the square
    for (int i = 1; i <= n; i++) {
         
    // To access columns of the square
    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))
        {
        // Prints star(*)           
        System.out.print("*");
        }
        else
        {
        // Prints space(" ")   
        System.out.print(" ");
        }
    }
 
    System.out.print("\n");
    }
}
 
// Driver code
public static void main(String[] args) {
    int n = 7;
    printPattern(n);
}
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to
# print square inside
# a square pattern
 
# Function to print
# the pattern square
# inside a square
def printPattern(n):
     
    # To access rows of the square
    for i in range(1,n+1):
     
        # To access columns of the square
        for j in range(1,n+1):
         
            # For printing the required square pattern
            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="") # Prints star(*)
             
            else:
             
                print(" ",end="") # Prints space(" ")
             
        print()
         
 
# Driver code
 
n = 7  
printPattern(n)
 
# This code is contributed
# by Anant Agarwal.


C#




// C# program to print square inside
// a square pattern
using System;
 
class GFG {
     
    // Function to print the pattern square
    // inside a square
    static void printPattern(int n) {
     
        // To access rows of the square
        for (int i = 1; i <= n; i++) {
             
            // To access columns of the square
            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))
                {
                     
                    // Prints star(*)    
                    Console.Write("*");
                }
                else
                {
                     
                    // Prints space(" ")
                    Console.Write(" ");
                }
            }
         
            Console.WriteLine();
        }
    }
     
    // Driver code
    public static void Main() {
         
        int n = 7;
         
        printPattern(n);
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to print square inside
// a square pattern
 
// Function to print the pattern square
// inside a square
function printPattern($n)
{
    // To access rows of the square
    for ($i = 1; $i <= $n; $i++)
    {
        // To access columns of the square
        for ($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))
            {
                // Prints star(*)
                echo "*";
            }
            else
            {
                // Prints space " "
                echo " ";
            }
        }
     
        echo"\n";
    }
}
 
// Driver Code
$n = 7;
printPattern($n);
 
// This code is contributed by Mithun Kumar
?>


Javascript




// JavaScript program to print square inside
// a square pattern
 
function printPattern(n) {
// To access rows of the square
for (let i = 1; i <= n; i++) {
     
    // To access columns of the square
    for (let 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))
        {
            // Prints star(*)           
            process.stdout.write("*");
        }
        else
        {
            // Prints space(" ")   
            process.stdout.write(" ");
        }
    }
 
    process.stdout.write("\n");
}
}
 
// Driver code
let n = 7;
printPattern(n);


Output : 
 

*******
*     *
* *** *
* * * *
* *** *
*     *
*******

 Time Complexity: O(n2)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS