Print diamond pattern with different layers

Here given code implementation process.

//  C program for
//  Print diamond pattern with different layers
#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("\nGiven n : %d \n", n);
    // Top half 
    for (int i = 0; i < n; ++i)
    {
        includeSpace(n - i);
        for (int j = 0; j <= i; ++j)
        {
            printf("%d", j);
        }
        for (int k = i - 1; k >= 0; --k)
        {
            printf("%d", k);
        }
        printf("\n");
    }
    // Bottom half 
    for (int i = n - 1; i >= 1; --i)
    {
        includeSpace(n + 1 - i);
        for (int j = 0; j < i; ++j)
        {
            printf("%d", j);
        }
        for (int k = i - 2; k >= 0; --k)
        {
            printf("%d", k);
        }
        printf("\n");
    }
}
int main(int argc, char
    const *argv[])
{
    // Test
    printPattern(5);
    printPattern(8);
    return 0;
}

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
/*
    Java Program
    Print diamond pattern with different layers
*/
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.println("\nGiven n : " + n);
		// Top half 
		for (int i = 0; i < n; ++i)
		{
			includeSpace(n - i);
			for (int j = 0; j <= i; ++j)
			{
				System.out.print(j);
			}
			for (int k = i - 1; k >= 0; --k)
			{
				System.out.print(k);
			}
			System.out.print("\n");
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			includeSpace(n + 1 - i);
			for (int j = 0; j < i; ++j)
			{
				System.out.print(j);
			}
			for (int k = i - 2; k >= 0; --k)
			{
				System.out.print(k);
			}
			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
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print diamond pattern with different layers
*/
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 << "\nGiven n : " << n << endl;
		// Top half 
		for (int i = 0; i < n; ++i)
		{
			this->includeSpace(n - i);
			for (int j = 0; j <= i; ++j)
			{
				cout << j;
			}
			for (int k = i - 1; k >= 0; --k)
			{
				cout << k;
			}
			cout << "\n";
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			this->includeSpace(n + 1 - i);
			for (int j = 0; j < i; ++j)
			{
				cout << j;
			}
			for (int k = i - 2; k >= 0; --k)
			{
				cout << k;
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(8);
	return 0;
}

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
// Include namespace system
using System;
/*
    Csharp Program
    Print diamond pattern with different layers
*/
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.WriteLine("\nGiven n : " + n);
		// Top half 
		for (int i = 0; i < n; ++i)
		{
			this.includeSpace(n - i);
			for (int j = 0; j <= i; ++j)
			{
				Console.Write(j);
			}
			for (int k = i - 1; k >= 0; --k)
			{
				Console.Write(k);
			}
			Console.Write("\n");
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			this.includeSpace(n + 1 - i);
			for (int j = 0; j < i; ++j)
			{
				Console.Write(j);
			}
			for (int k = i - 2; k >= 0; --k)
			{
				Console.Write(k);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(8);
	}
}

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
package main
import "fmt"
/*
    Go Program
    Print diamond pattern with different layers
*/
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.Println("\nGiven n : ", n)
	// Top half 
	for i := 0 ; i < n ; i++ {
		this.includeSpace(n - i)
		for j := 0 ; j <= i ; j++ {
			fmt.Print(j)
		}
		for k := i - 1 ; k >= 0 ; k-- {
			fmt.Print(k)
		}
		fmt.Print("\n")
	}
	// Bottom half 
	for i := n - 1 ; i >= 1 ; i-- {
		this.includeSpace(n + 1 - i)
		for j := 0 ; j < i ; j++ {
			fmt.Print(j)
		}
		for k := i - 2 ; k >= 0 ; k-- {
			fmt.Print(k)
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(8)
}

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
<?php
/*
    Php Program
    Print diamond pattern with different layers
*/
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("\nGiven n : ".$n."\n");
		// Top half 
		for ($i = 0; $i < $n; ++$i)
		{
			$this->includeSpace($n - $i);
			for ($j = 0; $j <= $i; ++$j)
			{
				echo($j);
			}
			for ($k = $i - 1; $k >= 0; --$k)
			{
				echo($k);
			}
			echo("\n");
		}
		// Bottom half 
		for ($i = $n - 1; $i >= 1; --$i)
		{
			$this->includeSpace($n + 1 - $i);
			for ($j = 0; $j < $i; ++$j)
			{
				echo($j);
			}
			for ($k = $i - 2; $k >= 0; --$k)
			{
				echo($k);
			}
			echo("\n");
		}
	}
}

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

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
/*
    Node JS Program
    Print diamond pattern with different layers
*/
class Pattern
{
	// This is display empty space of given length
	includeSpace(n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" ");
		}
	}
	printPattern(n)
	{
		console.log("\nGiven n : " + n);
		// Top half 
		for (var i = 0; i < n; ++i)
		{
			this.includeSpace(n - i);
			for (var j = 0; j <= i; ++j)
			{
				process.stdout.write("" + j);
			}
			for (var k = i - 1; k >= 0; --k)
			{
				process.stdout.write("" + k);
			}
			process.stdout.write("\n");
		}
		// Bottom half 
		for (var i = n - 1; i >= 1; --i)
		{
			this.includeSpace(n + 1 - i);
			for (var j = 0; j < i; ++j)
			{
				process.stdout.write("" + j);
			}
			for (var k = i - 2; k >= 0; --k)
			{
				process.stdout.write("" + k);
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(8);
}
main();

Output

Given n : 5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
#    Python 3 Program
#    Print diamond pattern with different layers
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("\nGiven n : ", n)
		i = 0
		#  Top half 
		while (i < n) :
			self.includeSpace(n - i)
			j = 0
			while (j <= i) :
				print(j, end = "")
				j += 1
			
			k = i - 1
			while (k >= 0) :
				print(k, end = "")
				k -= 1
			
			print(end = "\n")
			i += 1
		
		i = n - 1
		#  Bottom half 
		while (i >= 1) :
			self.includeSpace(n + 1 - i)
			j = 0
			while (j < i) :
				print(j, end = "")
				j += 1
			
			k = i - 2
			while (k >= 0) :
				print(k, end = "")
				k -= 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
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n :  8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
#    Ruby Program
#    Print diamond pattern with different layers
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("\nGiven n : ", n, "\n")
		i = 0
		#  Top half 
		while (i < n) 
			self.includeSpace(n - i)
			j = 0
			while (j <= i) 
				print(j)
				j += 1
			end

			k = i - 1
			while (k >= 0) 
				print(k)
				k -= 1
			end

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

		i = n - 1
		#  Bottom half 
		while (i >= 1) 
			self.includeSpace(n + 1 - i)
			j = 0
			while (j < i) 
				print(j)
				j += 1
			end

			k = i - 2
			while (k >= 0) 
				print(k)
				k -= 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
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
/*
    Scala Program
    Print diamond pattern with different layers
*/
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 = {
		println("\nGiven n : " + n);
		var i: Int = 0;
		// Top half 
		while (i < n)
		{
			includeSpace(n - i);
			var j: Int = 0;
			while (j <= i)
			{
				print(j);
				j += 1;
			}
			var k: Int = i - 1;
			while (k >= 0)
			{
				print(k);
				k -= 1;
			}
			print("\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			includeSpace(n + 1 - i);
			var j: Int = 0;
			while (j < i)
			{
				print(j);
				j += 1;
			}
			var k: Int = i - 2;
			while (k >= 0)
			{
				print(k);
				k -= 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
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
/*
    Swift 4 Program
    Print diamond pattern with different layers
*/
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("\nGiven n : ", n);
		var i: Int = 0;
		// Top half 
		while (i < n)
		{
			self.includeSpace(n - i);
			var j: Int = 0;
			while (j <= i)
			{
				print(j, terminator: "");
				j += 1;
			}
			var k: Int = i - 1;
			while (k >= 0)
			{
				print(k, terminator: "");
				k -= 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			self.includeSpace(n + 1 - i);
			var j: Int = 0;
			while (j < i)
			{
				print(j, terminator: "");
				j += 1;
			}
			var k: Int = i - 2;
			while (k >= 0)
			{
				print(k, terminator: "");
				k -= 1;
			}
			print(terminator: "\n");
			i -= 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(8);
}
main();

Output

Given n :  5
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n :  8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0
/*
    Kotlin Program
    Print diamond pattern with different layers
*/
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
	{
		println("\nGiven n : " + n);
		var i: Int = 0;
		// Top half 
		while (i < n)
		{
			this.includeSpace(n - i);
			var j: Int = 0;
			while (j <= i)
			{
				print(j);
				j += 1;
			}
			var k: Int = i - 1;
			while (k >= 0)
			{
				print(k);
				k -= 1;
			}
			print("\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			this.includeSpace(n + 1 - i);
			var j: Int = 0;
			while (j < i)
			{
				print(j);
				j += 1;
			}
			var k: Int = i - 2;
			while (k >= 0)
			{
				print(k);
				k -= 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
     0
    010
   01210
  0123210
 012343210
  0123210
   01210
    010
     0

Given n : 8
        0
       010
      01210
     0123210
    012343210
   01234543210
  0123456543210
 012345676543210
  0123456543210
   01234543210
    012343210
     0123210
      01210
       010
        0


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