Solid square inside a hollow square
Here given code implementation process.
// 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)
{
Pattern task = new Pattern();
// Test
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
}
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()
{
Pattern *task = new Pattern();
// Test
task->printPattern(7);
task->printPattern(12);
task->printPattern(15);
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)
{
Pattern task = new Pattern();
// Test
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
}
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
task.printPattern(7)
task.printPattern(12)
task.printPattern(15)
}
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()
{
$task = new Pattern();
// Test
$task->printPattern(7);
$task->printPattern(12);
$task->printPattern(15);
}
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()
{
var task = new Pattern();
// Test
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
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() :
task = Pattern()
# Test
task.printPattern(7)
task.printPattern(12)
task.printPattern(15)
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()
task = Pattern.new()
# Test
task.printPattern(7)
task.printPattern(12)
task.printPattern(15)
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
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
}
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()
{
let task: Pattern = Pattern();
// Test
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
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
{
val task: Pattern = Pattern();
// Test
task.printPattern(7);
task.printPattern(12);
task.printPattern(15);
}
Output
Given height : 7
*******
* *
* *** *
* *** *
* *** *
* *
*******
Given height : 12
************
* *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* ******** *
* *
************
Given height : 15
***************
* *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *********** *
* *
***************
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.
New Comment