Print the hollow diamond pattern

Here given code implementation process.

//  C program for
//  Print the hollow diamond pattern
#include <stdio.h>
 // This is display empty space of given length
void includeSpace(int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf(" ");
	}
}
void printPattern(int n)
{
	printf("\n Given n : %d \n", n);
	// Top Section
	for (int i = 0; i < n; ++i)
	{
		includeSpace((1 + n - i) *2);
		for (int j = 0; j <= i *2; ++j)
		{
			if (j == 0 || j == i *2)
			{
				printf("✰ ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
	// Bottom layer
	for (int i = 2; i <= n; ++i)
	{
		includeSpace(2 + i *2);
		for (int j = 0; j <= (n - i) *2; ++j)
		{
			if (j == 0 || j == (n - i) *2)
			{
				printf("✰ ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5);
	printPattern(8);
	return 0;
}

Output

 Given n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
/*
    Java Program
    Print the hollow diamond pattern
*/
public class Pattern
{
	// This is display empty space of given length
	public void includeSpace(int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" ");
		}
	}
	public void printPattern(int n)
	{
		System.out.print("\n Given n : " + n + " \n");
		// Top Section
		for (int i = 0; i < n; ++i)
		{
			includeSpace((1 + n - i) * 2);
			for (int j = 0; j <= i * 2; ++j)
			{
				if (j == 0 || j == i * 2)
				{
					System.out.print("✰ ");
				}
				else
				{
					System.out.print("  ");
				}
			}
			System.out.print("\n");
		}
		// Bottom layer
		for (int i = 2; i <= n; ++i)
		{
			includeSpace(2 + i * 2);
			for (int j = 0; j <= (n - i) * 2; ++j)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print the hollow diamond pattern
*/
class Pattern
{
	public:
		// This is display empty space of given length
		void includeSpace(int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " ";
			}
		}
	void printPattern(int n)
	{
		cout << "\n Given n : " << n << " \n";
		// Top Section
		for (int i = 0; i < n; ++i)
		{
			this->includeSpace((1 + n - i) *2);
			for (int j = 0; j <= i *2; ++j)
			{
				if (j == 0 || j == i *2)
				{
					cout << "✰ ";
				}
				else
				{
					cout << "  ";
				}
			}
			cout << "\n";
		}
		// Bottom layer
		for (int i = 2; i <= n; ++i)
		{
			this->includeSpace(2 + i *2);
			for (int j = 0; j <= (n - i) *2; ++j)
			{
				if (j == 0 || j == (n - i) *2)
				{
					cout << "✰ ";
				}
				else
				{
					cout << "  ";
				}
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(8);
	return 0;
}

Output

 Given n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
// Include namespace system
using System;
/*
    Csharp Program
    Print the hollow diamond pattern
*/
public class Pattern
{
	// This is display empty space of given length
	public void includeSpace(int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" ");
		}
	}
	public void printPattern(int n)
	{
		Console.Write("\n Given n : " + n + " \n");
		// Top Section
		for (int i = 0; i < n; ++i)
		{
			this.includeSpace((1 + n - i) * 2);
			for (int j = 0; j <= i * 2; ++j)
			{
				if (j == 0 || j == i * 2)
				{
					Console.Write("✰ ");
				}
				else
				{
					Console.Write("  ");
				}
			}
			Console.Write("\n");
		}
		// Bottom layer
		for (int i = 2; i <= n; ++i)
		{
			this.includeSpace(2 + i * 2);
			for (int j = 0; j <= (n - i) * 2; ++j)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
package main
import "fmt"
/*
    Go Program
    Print the hollow diamond pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
// This is display empty space of given length
func(this Pattern) includeSpace(n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ")
	}
}
func(this Pattern) printPattern(n int) {
	fmt.Print("\n Given n : ", n, " \n")
	// Top Section
	for i := 0 ; i < n ; i++ {
		this.includeSpace((1 + n - i) * 2)
		for j := 0 ; j <= i * 2 ; j++ {
			if j == 0 || j == i * 2 {
				fmt.Print("✰ ")
			} else {
				fmt.Print("  ")
			}
		}
		fmt.Print("\n")
	}
	// Bottom layer
	for i := 2 ; i <= n ; i++ {
		this.includeSpace(2 + i * 2)
		for j := 0 ; j <= (n - i) * 2 ; j++ {
			if j == 0 || j == (n - i) * 2 {
				fmt.Print("✰ ")
			} else {
				fmt.Print("  ")
			}
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(8)
}

Output

 Given n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
<?php
/*
    Php Program
    Print the hollow diamond pattern
*/
class Pattern
{
	// This is display empty space of given length
	public	function includeSpace($n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ");
		}
	}
	public	function printPattern($n)
	{
		echo("\n Given n : ".$n." \n");
		// Top Section
		for ($i = 0; $i < $n; ++$i)
		{
			$this->includeSpace((1 + $n - $i) * 2);
			for ($j = 0; $j <= $i * 2; ++$j)
			{
				if ($j == 0 || $j == $i * 2)
				{
					echo("✰ ");
				}
				else
				{
					echo("  ");
				}
			}
			echo("\n");
		}
		// Bottom layer
		for ($i = 2; $i <= $n; ++$i)
		{
			$this->includeSpace(2 + $i * 2);
			for ($j = 0; $j <= ($n - $i) * 2; ++$j)
			{
				if ($j == 0 || $j == ($n - $i) * 2)
				{
					echo("✰ ");
				}
				else
				{
					echo("  ");
				}
			}
			echo("\n");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(8);
}
main();

Output

 Given n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
/*
    Node JS Program
    Print the hollow diamond pattern
*/
class Pattern
{
	// This is display empty space of given length
	includeSpace(n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" ");
		}
	}
	printPattern(n)
	{
		process.stdout.write("\n Given n : " + n + " \n");
		// Top Section
		for (var i = 0; i < n; ++i)
		{
			this.includeSpace((1 + n - i) * 2);
			for (var j = 0; j <= i * 2; ++j)
			{
				if (j == 0 || j == i * 2)
				{
					process.stdout.write("✰ ");
				}
				else
				{
					process.stdout.write("  ");
				}
			}
			process.stdout.write("\n");
		}
		// Bottom layer
		for (var i = 2; i <= n; ++i)
		{
			this.includeSpace(2 + i * 2);
			for (var j = 0; j <= (n - i) * 2; ++j)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
#    Python 3 Program
#    Print the hollow diamond pattern
class Pattern :
	#  This is display empty space of given length
	def includeSpace(self, n) :
		i = 0
		while (i < n) :
			print(" ", end = "")
			i += 1
		
	
	def printPattern(self, n) :
		print("\n Given n : ", n ," ")
		i = 0
		#  Top Section
		while (i < n) :
			self.includeSpace((1 + n - i) * 2)
			j = 0
			while (j <= i * 2) :
				if (j == 0 or j == i * 2) :
					print("✰ ", end = "")
				else :
					print("  ", end = "")
				
				j += 1
			
			print(end = "\n")
			i += 1
		
		i = 2
		#  Bottom layer
		while (i <= n) :
			self.includeSpace(2 + i * 2)
			j = 0
			while (j <= (n - i) * 2) :
				if (j == 0 or j == (n - i) * 2) :
					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 n :  5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n :  8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
#    Ruby Program
#    Print the hollow diamond pattern
class Pattern 
	#  This is display empty space of given length
	def includeSpace(n) 
		i = 0
		while (i < n) 
			print(" ")
			i += 1
		end

	end

	def printPattern(n) 
		print("\n Given n : ", n ," \n")
		i = 0
		#  Top Section
		while (i < n) 
			self.includeSpace((1 + n - i) * 2)
			j = 0
			while (j <= i * 2) 
				if (j == 0 || j == i * 2) 
					print("✰ ")
				else
 
					print("  ")
				end

				j += 1
			end

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

		i = 2
		#  Bottom layer
		while (i <= n) 
			self.includeSpace(2 + i * 2)
			j = 0
			while (j <= (n - i) * 2) 
				if (j == 0 || j == (n - i) * 2) 
					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 n : 5 
            ✰ 
          ✰   ✰ 
        ✰       ✰ 
      ✰           ✰ 
    ✰               ✰ 
      ✰           ✰ 
        ✰       ✰ 
          ✰   ✰ 
            ✰ 

 Given n : 8 
                  ✰ 
                ✰   ✰ 
              ✰       ✰ 
            ✰           ✰ 
          ✰               ✰ 
        ✰                   ✰ 
      ✰                       ✰ 
    ✰                           ✰ 
      ✰                       ✰ 
        ✰                   ✰ 
          ✰               ✰ 
            ✰           ✰ 
              ✰       ✰ 
                ✰   ✰ 
                  ✰ 
/*
    Scala Program
    Print the hollow diamond pattern
*/
class Pattern()
{
	// This is display empty space of given length
	def includeSpace(n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" ");
			i += 1;
		}
	}
	def printPattern(n: Int): Unit = {
		print("\n Given n : " + n + " \n");
		var i: Int = 0;
		// Top Section
		while (i < n)
		{
			includeSpace((1 + n - i) * 2);
			var j: Int = 0;
			while (j <= i * 2)
			{
				if (j == 0 || j == i * 2)
				{
					print("✰ ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
		i = 2;
		// Bottom layer
		while (i <= n)
		{
			includeSpace(2 + i * 2);
			var j: Int = 0;
			while (j <= (n - i) * 2)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
/*
    Swift 4 Program
    Print the hollow diamond pattern
*/
class Pattern
{
	// This is display empty space of given length
	func includeSpace(_ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	func printPattern(_ n: Int)
	{
		print("\n Given n : ", n ," ");
		var i: Int = 0;
		// Top Section
		while (i < n)
		{
			self.includeSpace((1 + n - i) * 2);
			var j: Int = 0;
			while (j <= i * 2)
			{
				if (j == 0 || j == i * 2)
				{
					print("✰ ", terminator: "");
				}
				else
				{
					print("  ", terminator: "");
				}
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		i = 2;
		// Bottom layer
		while (i <= n)
		{
			self.includeSpace(2 + i * 2);
			var j: Int = 0;
			while (j <= (n - i) * 2)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n :  5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n :  8
                  ✰
                ✰   ✰
              ✰       ✰
            ✰           ✰
          ✰               ✰
        ✰                   ✰
      ✰                       ✰
    ✰                           ✰
      ✰                       ✰
        ✰                   ✰
          ✰               ✰
            ✰           ✰
              ✰       ✰
                ✰   ✰
                  ✰
/*
    Kotlin Program
    Print the hollow diamond pattern
*/
class Pattern
{
	// This is display empty space of given length
	fun includeSpace(n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ");
			i += 1;
		}
	}
	fun printPattern(n: Int): Unit
	{
		print("\n Given n : " + n + " \n");
		var i: Int = 0;
		// Top Section
		while (i < n)
		{
			this.includeSpace((1 + n - i) * 2);
			var j: Int = 0;
			while (j <= i * 2)
			{
				if (j == 0 || j == i * 2)
				{
					print("✰ ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
		i = 2;
		// Bottom layer
		while (i <= n)
		{
			this.includeSpace(2 + i * 2);
			var j: Int = 0;
			while (j <= (n - i) * 2)
			{
				if (j == 0 || j == (n - i) * 2)
				{
					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 n : 5
            ✰
          ✰   ✰
        ✰       ✰
      ✰           ✰
    ✰               ✰
      ✰           ✰
        ✰       ✰
          ✰   ✰
            ✰

 Given n : 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