Posted on by Kalkicode
Code Pattern

# Print hollow half diamond hash pattern

In this article, we will discuss how to print a hollow half diamond hash pattern using a simple C program. The pattern consists of asterisks (*) arranged in a specific manner to form a diamond shape with hollow spaces in between. We will explain the problem statement, provide a suitable example, discuss the algorithm and pseudocode, and analyze the resultant output with the time complexity of the code.

### Problem Statement

The problem is to write a program that prints a hollow half diamond hash pattern based on the given width. The pattern should be displayed on the console using asterisks (*) and spaces. The top half of the diamond should be printed first, followed by the bottom half. Each row of the pattern should have asterisks (*) at the edges and spaces in between. The width determines the number of rows in the pattern.

#### Example:

Let's consider an example with a width of 5. The expected pattern is as follows:

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

### Algorithm

To solve this problem, we can use a nested loop structure in the program. The outer loop is responsible for iterating over the rows, and the inner loop prints the asterisks (*) and spaces for each row.

1. Start the outer loop from 0 and iterate up to width-1 to print the top half of the diamond.
2. Inside the outer loop, start the inner loop from 0 and iterate up to the current row (i) to print the asterisks (*) and spaces.
3. For each iteration, check if the current column (j) is the first column (j == 0) or the last column (j == i). If it is, print an asterisk (*); otherwise, print a space.
4. After completing the inner loop, print a new line to move to the next row.
5. Repeat steps 2-4 for the bottom half of the diamond. Start the outer loop from width-2 and iterate down to 0.
6. End the program.

### Pseudocode

```printPattern(width):
Print "Given width: width"
// Top half of the diamond
for i = 0 to width-1:
for j = 0 to i:
if j = 0 or j = i:
Print "*"
else:
Print " "
Print new line

// Bottom half of the diamond
for i = width-2 down to 0:
for j = 0 to i:
if j = 0 or j = i:
Print "*"
else:
Print " "
Print new line

main():
Call printPattern(5)
Call printPattern(8)
```

## Code Solution

``````//  C program for
//  Print hollow half diamond hash pattern
#include <stdio.h>

void printPattern(int width)
{
printf("\nGiven width : %d \n", width);
// Top half
for (int i = 0; i < width; ++i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// Bottom half
for (int i = width - 2; i >= 0; --i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(5);
printPattern(8);
return 0;
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````/*
Java Program
Print hollow half diamond hash pattern
*/
public class Pattern
{
public void printPattern(int width)
{
System.out.println("\nGiven width : " + width);
// Top half
for (int i = 0; i < width; ++i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
// Bottom half
for (int i = width - 2; i >= 0; --i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print hollow half diamond hash pattern
*/
class Pattern
{
public: void printPattern(int width)
{
cout << "\nGiven width : " << width << endl;
// Top half
for (int i = 0; i < width; ++i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
// Bottom half
for (int i = width - 2; i >= 0; --i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````// Include namespace system
using System;
/*
Csharp Program
Print hollow half diamond hash pattern
*/
public class Pattern
{
public void printPattern(int width)
{
Console.WriteLine("\nGiven width : " + width);
// Top half
for (int i = 0; i < width; ++i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
// Bottom half
for (int i = width - 2; i >= 0; --i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````package main
import "fmt"
/*
Go Program
Print hollow half diamond hash pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
func(this Pattern) printPattern(width int) {
fmt.Println("\nGiven width : ", width)
// Top half
for i := 0 ; i < width ; i++ {
for j := 0 ; j <= i ; j++ {
if j == 0 || j == i {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
// Bottom half
for i := width - 2 ; i >= 0 ; i-- {
for j := 0 ; j <= i ; j++ {
if j == 0 || j == i {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````<?php
/*
Php Program
Print hollow half diamond hash pattern
*/
class Pattern
{
public	function printPattern(\$width)
{
echo("\nGiven width : ".\$width."\n");
// Top half
for (\$i = 0; \$i < \$width; ++\$i)
{
for (\$j = 0; \$j <= \$i; ++\$j)
{
if (\$j == 0 || \$j == \$i)
{
echo("*");
}
else
{
echo(" ");
}
}
echo("\n");
}
// Bottom half
for (\$i = \$width - 2; \$i >= 0; --\$i)
{
for (\$j = 0; \$j <= \$i; ++\$j)
{
if (\$j == 0 || \$j == \$i)
{
echo("*");
}
else
{
echo(" ");
}
}
echo("\n");
}
}
}

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

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````/*
Node JS Program
Print hollow half diamond hash pattern
*/
class Pattern
{
printPattern(width)
{
console.log("\nGiven width : " + width);
// Top half
for (var i = 0; i < width; ++i)
{
for (var j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
// Bottom half
for (var i = width - 2; i >= 0; --i)
{
for (var j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}

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

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````#    Python 3 Program
#    Print hollow half diamond hash pattern
class Pattern :
def printPattern(self, width) :
print("\nGiven width : ", width)
i = 0
#  Top half
while (i < width) :
j = 0
while (j <= i) :
if (j == 0 or j == i) :
print("*", end = "")
else :
print(" ", end = "")

j += 1

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

i = width - 2
#  Bottom half
while (i >= 0) :
j = 0
while (j <= i) :
if (j == 0 or j == i) :
print("*", end = "")
else :
print(" ", end = "")

j += 1

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

def main() :
#  Test

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

#### Output

``````Given width :  5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width :  8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````#    Ruby Program
#    Print hollow half diamond hash pattern
class Pattern
def printPattern(width)
print("\nGiven width : ", width, "\n")
i = 0
#  Top half
while (i < width)
j = 0
while (j <= i)
if (j == 0 || j == i)
print("*")
else

print(" ")
end

j += 1
end

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

i = width - 2
#  Bottom half
while (i >= 0)
j = 0
while (j <= i)
if (j == 0 || j == i)
print("*")
else

print(" ")
end

j += 1
end

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

end

end

def main()
#  Test
end

main()``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*
``````
``````/*
Scala Program
Print hollow half diamond hash pattern
*/
class Pattern()
{
def printPattern(width: Int): Unit = {
println("\nGiven width : " + width);
var i: Int = 0;
// Top half
while (i < width)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
i = width - 2;
// Bottom half
while (i >= 0)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
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 width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````/*
Swift 4 Program
Print hollow half diamond hash pattern
*/
class Pattern
{
func printPattern(_ width: Int)
{
print("\nGiven width : ", width);
var i: Int = 0;
// Top half
while (i < width)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
i = width - 2;
// Bottom half
while (i >= 0)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i -= 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

``````Given width :  5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width :  8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````
``````/*
Kotlin Program
Print hollow half diamond hash pattern
*/
class Pattern
{
fun printPattern(width: Int): Unit
{
println("\nGiven width : " + width);
var i: Int = 0;
// Top half
while (i < width)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
i = width - 2;
// Bottom half
while (i >= 0)
{
var j: Int = 0;
while (j <= i)
{
if (j == 0 || j == i)
{
print("*");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i -= 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

``````Given width : 5
*
**
* *
*  *
*   *
*  *
* *
**
*

Given width : 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    *
*   *
*  *
* *
**
*``````

### Resultant Output Explanation

Given width: 5 The pattern is printed in the following manner:

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

Each row of the pattern starts and ends with an asterisk (*) while the spaces in between create a hollow effect. The top half of the diamond is printed first, followed by the bottom half.

Given width: 8 The pattern is printed as follows:

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

Similarly, each row begins and ends with an asterisk (*), forming a diamond shape with hollow spaces. The top half is printed first, then the bottom half.

### Time Complexity

The time complexity of this program is O(n^2), where n is the width of the pattern. This is because we have two nested loops that iterate from 0 to n-1, resulting in a quadratic time complexity.

## 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.

Categories
Relative Post