Print numbers with diamond pattern

Here given code implementation process.

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

Output

Given n : 5
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
/*
    Java Program
    Print numbers with 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.println("\nGiven n : " + n);
		// Top half 
		for (int i = 1; i <= n; ++i)
		{
			includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				System.out.print(j);
			}
			for (int k = 2; k <= i; ++k)
			{
				System.out.print(k);
			}
			System.out.print("\n");
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				System.out.print(j);
			}
			for (int k = 2; k <= i; ++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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print numbers with 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 << "\nGiven n : " << n << endl;
		// Top half 
		for (int i = 1; i <= n; ++i)
		{
			this->includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				cout << j;
			}
			for (int k = 2; k <= i; ++k)
			{
				cout << k;
			}
			cout << "\n";
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			this->includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				cout << j;
			}
			for (int k = 2; k <= i; ++k)
			{
				cout << k;
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(8);
	return 0;
}

Output

Given n : 5
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
// Include namespace system
using System;
/*
    Csharp Program
    Print numbers with 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.WriteLine("\nGiven n : " + n);
		// Top half 
		for (int i = 1; i <= n; ++i)
		{
			this.includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				Console.Write(j);
			}
			for (int k = 2; k <= i; ++k)
			{
				Console.Write(k);
			}
			Console.Write("\n");
		}
		// Bottom half 
		for (int i = n - 1; i >= 1; --i)
		{
			this.includeSpace(n - i);
			for (int j = i; j >= 1; --j)
			{
				Console.Write(j);
			}
			for (int k = 2; k <= i; ++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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
package main
import "fmt"
/*
    Go Program
    Print numbers with diamond pattern
*/

// This is display empty space of given length
func includeSpace(n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ")
	}
}
func printPattern(n int) {
	fmt.Println("\nGiven n : ", n)
	// Top half 
	for i := 1 ; i <= n ; i++ {
		includeSpace(n - i)
		for j := i ; j >= 1 ; j-- {
			fmt.Print(j)
		}
		for k := 2 ; k <= i ; k++ {
			fmt.Print(k)
		}
		fmt.Print("\n")
	}
	// Bottom half 
	for i := n - 1 ; i >= 1 ; i-- {
		includeSpace(n - i)
		for j := i ; j >= 1 ; j-- {
			fmt.Print(j)
		}
		for k := 2 ; k <= i ; k++ {
			fmt.Print(k)
		}
		fmt.Print("\n")
	}
}
func main() {
	
	// Test
	printPattern(5)
	printPattern(8)
}

Output

Given n : 5
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
<?php
/*
    Php Program
    Print numbers with 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("\nGiven n : ".$n."\n");
		// Top half 
		for ($i = 1; $i <= $n; ++$i)
		{
			$this->includeSpace($n - $i);
			for ($j = $i; $j >= 1; --$j)
			{
				echo($j);
			}
			for ($k = 2; $k <= $i; ++$k)
			{
				echo($k);
			}
			echo("\n");
		}
		// Bottom half 
		for ($i = $n - 1; $i >= 1; --$i)
		{
			$this->includeSpace($n - $i);
			for ($j = $i; $j >= 1; --$j)
			{
				echo($j);
			}
			for ($k = 2; $k <= $i; ++$k)
			{
				echo($k);
			}
			echo("\n");
		}
	}
}

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

Output

Given n : 5
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
/*
    Node JS Program
    Print numbers with 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)
	{
		console.log("\nGiven n : " + n);
		// Top half 
		for (var i = 1; i <= n; ++i)
		{
			this.includeSpace(n - i);
			for (var j = i; j >= 1; --j)
			{
				process.stdout.write("" + j);
			}
			for (var k = 2; k <= i; ++k)
			{
				process.stdout.write("" + k);
			}
			process.stdout.write("\n");
		}
		// Bottom half 
		for (var i = n - 1; i >= 1; --i)
		{
			this.includeSpace(n - i);
			for (var j = i; j >= 1; --j)
			{
				process.stdout.write("" + j);
			}
			for (var k = 2; k <= i; ++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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
#    Python 3 Program
#    Print numbers with 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("\nGiven n : ", n)
		i = 1
		#  Top half 
		while (i <= n) :
			self.includeSpace(n - i)
			j = i
			while (j >= 1) :
				print(j, end = "")
				j -= 1
			
			k = 2
			while (k <= i) :
				print(k, end = "")
				k += 1
			
			print(end = "\n")
			i += 1
		
		i = n - 1
		#  Bottom half 
		while (i >= 1) :
			self.includeSpace(n - i)
			j = i
			while (j >= 1) :
				print(j, end = "")
				j -= 1
			
			k = 2
			while (k <= i) :
				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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n :  8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
#    Ruby Program
#    Print numbers with 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("\nGiven n : ", n, "\n")
		i = 1
		#  Top half 
		while (i <= n) 
			self.includeSpace(n - i)
			j = i
			while (j >= 1) 
				print(j)
				j -= 1
			end

			k = 2
			while (k <= i) 
				print(k)
				k += 1
			end

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

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

			k = 2
			while (k <= i) 
				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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
/*
    Scala Program
    Print numbers with 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 = {
		println("\nGiven n : " + n);
		var i: Int = 1;
		// Top half 
		while (i <= n)
		{
			includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j);
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				print(k);
				k += 1;
			}
			print("\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j);
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
/*
    Swift 4 Program
    Print numbers with 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("\nGiven n : ", n);
		var i: Int = 1;
		// Top half 
		while (i <= n)
		{
			self.includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j, terminator: "");
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				print(k, terminator: "");
				k += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			self.includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j, terminator: "");
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n :  8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1
/*
    Kotlin Program
    Print numbers with 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
	{
		println("\nGiven n : " + n);
		var i: Int = 1;
		// Top half 
		while (i <= n)
		{
			this.includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j);
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				print(k);
				k += 1;
			}
			print("\n");
			i += 1;
		}
		i = n - 1;
		// Bottom half 
		while (i >= 1)
		{
			this.includeSpace(n - i);
			var j: Int = i;
			while (j >= 1)
			{
				print(j);
				j -= 1;
			}
			var k: Int = 2;
			while (k <= i)
			{
				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
    1
   212
  32123
 4321234
543212345
 4321234
  32123
   212
    1

Given n : 8
       1
      212
     32123
    4321234
   543212345
  65432123456
 7654321234567
876543212345678
 7654321234567
  65432123456
   543212345
    4321234
     32123
      212
       1

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