Solid square inside a hollow square | Pattern - GeeksforGeeks

08 Apr, 2021
Given the value of n, print a hollow square of side length n and inside it a solid square of side length (n – 4) using stars(*). Examples :  
Input : n = 6
Output :
******
*    *
* ** *
* ** *
*    *
******

Input : n = 11
Output :
***********
*         *
* ******* *
* ******* *
* ******* *
* ******* *
* ******* *
* ******* *
* ******* *
*         *
***********
 
 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to print// solid square inside a hollow square#include <bits/stdc++.h>using namespace std; // function to print the required patternvoid print_Pattern(int n){    for (int i = 1; i <= n; i++) {    for (int j = 1; j <= n; j++) {             // First two conditions are for outer    // hollow square and next two conditions    // are for inner solid square    if ((i == 1 || i == n) || (j == 1 || j == n) ||        (i >= 3 && i <= n - 2) && (j >= 3 && j <= n - 2))        cout<< "*";     else        cout<< " ";    }     cout<<endl;}} // Driver Codeint main(){    // Take n as input    int n = 6;      // function calling    print_Pattern(n);         return 0;}
// Java implementation to print// solid square inside a hollow squareimport java.io.*; class GFG{    // function to print the required pattern    static void print_Pattern(int n)    {        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= n; j++)            {                             // First two conditions are for outer            // hollow square and next two conditions            // are for inner solid square            if ((i == 1 || i == n) || (j == 1 || j == n) ||                (i >= 3 && i <= n - 2) && (j >= 3 && j <= n - 2))                System.out.print("*");                     else                System.out.print(" ");            }             System.out.println();        }    }         // Driver Code    public static void main (String[] args)    {        // Take n as input        int n = 6;                 // function calling        print_Pattern(n);             }} // This code is contributed by vt_m.
# Python3 implementation to print# solid square inside a hollow square # Function to print the required patterndef print_Pattern(n):     for i in range(1, n + 1):        for j in range(1, n + 1):                 # First two conditions are for outer        # hollow square and next two conditions        # are for inner solid square            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)):                print("*", end = "")             else:                print(end = " ")         print() # Driver Coden = 6print_Pattern(n)     # This code is contributed by Azkia Anam.
// C# implementation to print solid square// inside a hollow squareusing System; class GFG {         // function to print the required    // pattern    static void print_Pattern(int n)    {        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= n; j++)            {                             // First two conditions are            // for outer hollow square            // and next two conditions            // are for inner solid square            if ((i == 1 || i == n) ||                (j == 1 || j == n) ||               (i >= 3 && i <= n - 2) &&                 (j >= 3 && j <= n - 2))                Console.Write("*");                     else                Console.Write(" ");            }                 Console.WriteLine();        }    }         // Driver Code    public static void Main ()    {                 // Take n as input        int n = 6;                 // function calling        print_Pattern(n);    }} // This code is contributed by vt_m.
<?php// PHP implementation to print// solid square inside a hollow square // Function to print the required patternfunction print_Pattern($n){    for ($i = 1; $i <= $n; $i++)    {        for ($j = 1; $j <= $n; $j++)        {                     // First two conditions are            // for outer hollow square and            // next two conditions are for            // inner solid square            if (($i == 1 || $i == $n) ||                ($j == 1 || $j == $n) ||                ($i >= 3 && $i <= $n - 2) &&                ($j >= 3 && $j <= $n - 2))                echo "*";                     else                echo " ";        }        echo "\n";    }} // Driver Code$n = 6;print_Pattern($n); // This code is contributed by Mithun Kumar?>
<script>      // JavaScript implementation to print      // solid square inside a hollow square       // function to print the required pattern      function print_Pattern(n)      {        for (var i = 1; i <= n; i++)        {          for (var j = 1; j <= n; j++)          {                       // First two conditions are for outer            // hollow square and next two conditions            // are for inner solid square            if (              i == 1 ||              i == n ||              j == 1 ||              j == n ||              (i >= 3 && i <= n - 2 && j >= 3 && j <= n - 2)            )              document.write("*");            else document.write("  ");          }           document.write("<br>");        }      }       // Driver Code      // Take n as input      var n = 6;       // function calling      print_Pattern(n);             // This code is contributed by rdtank.    </script>
Output :  
******
*    *
* ** *
* ** *
*    *
******
 
Mithun Kumar
rdtank
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Ternary Operator in Python
Difference between Abstract Class and Interface in Java
Destructors in C++
Inline Functions in C++
Exception Handling in C++
Pure Virtual Functions and Abstract Classes in C++
Python Exception Handling
'this' pointer in C++