Print hollow half diamond hash pattern

Here given code implementation process.

//  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)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(8);
	}
}

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()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(8);
	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)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(8);
	}
}

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
	task.printPattern(5)
	task.printPattern(8)
}

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()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(8);
}
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()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(8);
}
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() :
	task = Pattern()
	#  Test
	task.printPattern(5)
	task.printPattern(8)

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() 
	task = Pattern.new()
	#  Test
	task.printPattern(5)
	task.printPattern(8)
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
		task.printPattern(5);
		task.printPattern(8);
	}
}

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()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(8);
}
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
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(8);
}

Output

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

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


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







© 2021, kalkicode.com, All rights reserved