Solid square inside a hollow square | Pattern
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++
// C++ implementation to print // solid square inside a hollow square #include <bits/stdc++.h> using namespace std; // function to print the required pattern 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)) cout<< "*"; else cout<< " "; } cout<<endl; } } // Driver Code int main() { // Take n as input int n = 6; // function calling print_Pattern(n); return 0; } |
chevron_right
filter_none
Java
// Java implementation to print // solid square inside a hollow square import 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. |
chevron_right
filter_none
Python3
# Python3 implementation to print # solid square inside a hollow square # Function to print the required pattern def 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 Code n = 6print_Pattern(n) # This code is contributed by Azkia Anam. |
chevron_right
filter_none
C#
// C# implementation to print solid square // inside a hollow square using 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. |
chevron_right
filter_none
PHP
<?php // PHP implementation to print // solid square inside a hollow square // Function to print the required pattern function 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 ?> |
chevron_right
filter_none
Output :
****** * * * ** * * ** * * * ******
Recommended Posts:
- Program to print solid and hollow square patterns
- Program to print Square inside a Square
- Program to print hollow rectangle or square star patterns
- Program to print solid and hollow rhombus patterns
- Java Program to Print a Square Pattern for given integer
- Hollow lower triangular pattern
- Program to print hollow Triangle pattern
- Program to print the pattern with two hollow Triangles
- Program to print hollow pyramid and diamond pattern
- Pattern of 1's inside 0's
- Perfect Square String
- Program for Area Of Square
- N-th number which is both a square and a cube
- Area of a square from diagonal length
- Area of square Circumscribed by Circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



