# Solid square inside a hollow square

In this article, we will explore the problem of creating a solid square inside a hollow square using the C programming language. We will provide an explanation of the problem statement, provide a suitable example, discuss the algorithm and pseudocode, and finally explain the resultant output with the time complexity of the code.

### Problem Statement

The problem requires us to print a pattern consisting of a solid square inside a hollow square. The user provides the height of the pattern, which represents the number of rows and columns in the pattern.

### Example

Let's consider an example to better understand the problem. Suppose we want to create a pattern with a height of 7:

Given height: 7

```*******
*     *
* *** *
* *** *
* *** *
*     *
*******
```

In the example, the solid square is represented by asterisks (*) and the hollow square is represented by spaces. The pattern has a total height of 7 rows and 7 columns.

### Algorithm

To solve this problem, we can use a nested loop structure to iterate over each cell in the pattern. The outer loop controls the row operation, and the inner loop controls the column operation.

1. Accept the height of the pattern from the user.
2. Print the given height.
3. Iterate over each cell in the pattern using nested loops:
• If the current cell is on the outer boundary of the pattern or inside the solid square, print an asterisk (*).
• Otherwise, print a space.
4. Print a new line after each row.
5. Repeat the process for all rows and columns in the pattern.

### Pseudocode

``````
function printPattern(height):
print "Given height: ", height
for i = 0 to height-1:
for j = 0 to height-1:
if (i == 0 || i + 1 == height || j == 0 || j + 1 == height || (j > 1 && i > 1 && i + 2 < height && j + 2 < height)):
print "*"
else:
print " "
print new line```
```

## Code Solution

``````//  C program for
//  Solid square inside a hollow square
#include <stdio.h>

void printPattern(int height)
{
printf("\nGiven height : %d \n", height);
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Inner loop control the column operation
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main(int argc, char const *argv[])
{
// Test
printPattern(7);
printPattern(12);
printPattern(15);
return 0;
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````/*
Java Program
Solid square inside a hollow square
*/
public class Pattern
{
public void printPattern(int height)
{
System.out.println("\nGiven height : " + height );
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Inner loop control the column operation
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Solid square inside a hollow square
*/
class Pattern
{
public: void printPattern(int height)
{
cout << "\nGiven height : "
<< height << endl;
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Inner loop control the column operation
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````// Include namespace system
using System;
/*
Csharp Program
Solid square inside a hollow square
*/
public class Pattern
{
public void printPattern(int height)
{
Console.WriteLine("\nGiven height : " + height);
// Outer loop control the row operation
for (int i = 0; i < height; ++i)
{
// Inner loop control the column operation
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````package main
import "fmt"
/*
Go Program
Solid square inside a hollow square
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
func(this Pattern) printPattern(height int) {
fmt.Println("\nGiven height : ", height)
// Outer loop control the row operation
for i := 0 ; i < height ; i++ {
// Inner loop control the column operation
for j := 0 ; j < height ; j++ {
if i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height) {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````<?php
/*
Php Program
Solid square inside a hollow square
*/
class Pattern
{
public	function printPattern(\$height)
{
echo("\nGiven height : ".\$height.
"\n");
// Outer loop control the row operation
for (\$i = 0; \$i < \$height; ++\$i)
{
// Inner loop control the column operation
for (\$j = 0; \$j < \$height; ++\$j)
{
if (\$i == 0 || \$i + 1 == \$height ||
\$j == 0 || \$j + 1 == \$height ||
(\$j > 1 && \$i > 1 && \$i + 2 < \$height && \$j + 2 < \$height))
{
echo("*");
}
else
{
echo(" ");
}
}
echo("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````/*
Node JS Program
Solid square inside a hollow square
*/
class Pattern
{
printPattern(height)
{
console.log("\nGiven height : " + height);
// Outer loop control the row operation
for (var i = 0; i < height; ++i)
{
// Inner loop control the column operation
for (var j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````#    Python 3 Program
#    Solid square inside a hollow square
class Pattern :
def printPattern(self, height) :
print("\nGiven height : ", height)
i = 0
#  Outer loop control the row operation
while (i < height) :
j = 0
#  Inner loop control the column operation
while (j < height) :
if (i == 0 or i + 1 == height or
j == 0 or j + 1 == height or
(j > 1 and i > 1 and i + 2 < height and j + 2 < height)) :
print("*", end = "")
else :
print(" ", end = "")

j += 1

print(end = "\n")
i += 1

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

``````Given height :  7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height :  12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height :  15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````#    Ruby Program
#    Solid square inside a hollow square
class Pattern
def printPattern(height)
print("\nGiven height : ", height, "\n")
i = 0
#  Outer loop control the row operation
while (i < height)
j = 0
#  Inner loop control the column operation
while (j < height)
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
print("*")
else

print(" ")
end

j += 1
end

print("\n")
i += 1
end

end

end

def main()
#  Test
end

main()``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************
``````
``````/*
Scala Program
Solid square inside a hollow square
*/
class Pattern()
{
def printPattern(height: Int): Unit = {
println("\nGiven height : " + height);
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
var j: Int = 0;
// Inner loop control the column operation
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
}
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````/*
Swift 4 Program
Solid square inside a hollow square
*/
class Pattern
{
func printPattern(_ height: Int)
{
print("\nGiven height : ", height);
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
var j: Int = 0;
// Inner loop control the column operation
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

``````Given height :  7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height :  12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height :  15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````
``````/*
Kotlin Program
Solid square inside a hollow square
*/
class Pattern
{
fun printPattern(height: Int): Unit
{
println("\nGiven height : " + height);
var i: Int = 0;
// Outer loop control the row operation
while (i < height)
{
var j: Int = 0;
// Inner loop control the column operation
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(j > 1 && i > 1 && i + 2 < height && j + 2 < height))
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

``````Given height : 7
*******
*     *
* *** *
* *** *
* *** *
*     *
*******

Given height : 12
************
*          *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
*          *
************

Given height : 15
***************
*             *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
*             *
***************``````

### Resultant Output Explanation

The resultant output consists of the pattern created using the provided code. For each given height, the code generates a square pattern with a solid square inside a hollow square. The solid square is represented by asterisks (*) and the hollow square is represented by spaces.

The time complexity of the code is O(n^2), where n is the height of the pattern. This is because the code uses nested loops to iterate over each cell in the pattern, resulting in n^2 iterations.

By following the provided algorithm and using the given code, you can generate solid squares inside hollow squares of various heights.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.