Posted on by Kalkicode
Code Pattern

Print the diamond shape side by side

In this article, we will discuss a C program that prints diamond shapes side by side. The program takes the height and the number of diamonds to be displayed as input. We will explain the problem statement, provide a suitable example, present the algorithm and pseudocode with explanations, and discuss the resultant output with time complexity analysis.

Problem Statement

The task is to print diamond shapes side by side. The program should take two inputs: the height of each diamond and the number of diamonds to be displayed. The program should print the diamond shapes in a side-by-side fashion.

Example

Let's consider an example where the height is 5 and the number of diamonds is 5. The expected output will be as follows:

    Height: 5  X: 5
  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

Algorithm

  1. Define a function includeSpace(n) that prints n number of empty spaces.
  2. Define a function printPattern(height, width) that prints the diamond shape.
  3. In the printPattern() function:
    1. Check if the height is even. If it is, return.
    2. Print the height and width for debugging purposes.
    3. Print the top layer of the diamond:
      1. Iterate from 0 to height/2 (exclusive):
        1. Iterate from 0 to width:
          1. Call includeSpace((height / 2) - i) to print the required number of spaces.
          2. Iterate from 0 to i (inclusive):
            1. Print the current number (j + 1) % 10.
          3. Call includeSpace((height / 2) - i) to print the required number of spaces.
        2. Print a new line.
    4. Print the bottom layer of the diamond:
      1. Iterate from height/2 to 0 (inclusive):
        1. Iterate from 0 to width:
          1. Call includeSpace((height / 2) - i) to print the required number of spaces.
          2. Iterate from 0 to i (inclusive):
            1. Print the current number (j + 1) % 10.
          3. Call includeSpace((height / 2) - i) to print the required number of spaces.
        2. Print a new line.
  4. In the main function:
    1. Call printPattern(5, 5) to print the diamond shape with height 5 and width 5.
    2. Call printPattern(9, 3) to print the diamond shape with height 9 and width 3.

Pseudocode

includeSpace(n):
  for i from 0 to n-1:
    print " "

printPattern(height, width):
  if height is even:
    return
  print "Height: ", height, " Width: ", width
  // Print top layer
  for i from 0 to height/2-1:
    for j from 0 to width-1:
      includeSpace((height/2) - i)
      for k from 0 to i:
        print (j + 1) % 10, " "
      includeSpace((height/2) - i)
    print new line
  // Print bottom layer
  for i from height/2 to 0:
    for j from 0 to width-1:
      includeSpace((height/2) - i)
      for k from 0 to i:
        print (j + 1) % 10, " "
      includeSpace((height/2) - i)
    print new line

main:
  printPattern(5, 5)
  printPattern(9, 3)

Code Solution

//  C program for
//  Print the diamond shape side by side
#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 height, int x)
{
	if (height % 2 == 0)
	{
		return;
	}
	printf("\n Height : %d  X : %d \n\n", height, x);
	// Print top layer
	for (int i = 0; i < height / 2; ++i)
	{
		for (int j = 0; j < x; ++j)
		{
			includeSpace((height / 2) - i);
			for (int k = 0; k <= i; ++k)
			{
				printf("%d ", (j + 1) % 10);
			}
			includeSpace((height / 2) - i);
		}
		printf("\n");
	}
	// Print bottom layer
	for (int i = height / 2; i >= 0; --i)
	{
		for (int j = 0; j < x; ++j)
		{
			includeSpace((height / 2) - i);
			for (int k = 0; k <= i; ++k)
			{
				printf("%d ", (j + 1) % 10);
			}
			includeSpace((height / 2) - i);
		}
		printf("\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5, 5);
	printPattern(9, 3);
	return 0;
}

Output

 Height : 5  X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9  X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
/*
    Java Program
    Print the diamond shape side by side
*/
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 height, int x)
	{
		if (height % 2 == 0)
		{
			return;
		}
		System.out.print("\n Height : " + height + " X : " + x + " \n\n");
		// Print top layer
		for (int i = 0; i < height / 2; ++i)
		{
			for (int j = 0; j < x; ++j)
			{
				includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					System.out.print((j + 1) % 10);
					System.out.print(" ");
				}
				includeSpace((height / 2) - i);
			}
			System.out.print("\n");
		}
		// Print bottom layer
		for (int i = height / 2; i >= 0; --i)
		{
			for (int j = 0; j < x; ++j)
			{
				includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					System.out.print((j + 1) % 10);
					System.out.print(" ");
				}
				includeSpace((height / 2) - i);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5, 5);
		task.printPattern(9, 3);
	}
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print the diamond shape side by side
*/
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 height, int x)
	{
		if (height % 2 == 0)
		{
			return;
		}
		cout << "\n Height : " << height << " X : " << x << " \n\n";
		// Print top layer
		for (int i = 0; i < height / 2; ++i)
		{
			for (int j = 0; j < x; ++j)
			{
				this->includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					cout << (j + 1) % 10;
					cout << " ";
				}
				this->includeSpace((height / 2) - i);
			}
			cout << "\n";
		}
		// Print bottom layer
		for (int i = height / 2; i >= 0; --i)
		{
			for (int j = 0; j < x; ++j)
			{
				this->includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					cout << (j + 1) % 10;
					cout << " ";
				}
				this->includeSpace((height / 2) - i);
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5, 5);
	task->printPattern(9, 3);
	return 0;
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
// Include namespace system
using System;
/*
    Csharp Program
    Print the diamond shape side by side
*/
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 height, int x)
	{
		if (height % 2 == 0)
		{
			return;
		}
		Console.Write("\n Height : " + height + " X : " + x + " \n\n");
		// Print top layer
		for (int i = 0; i < height / 2; ++i)
		{
			for (int j = 0; j < x; ++j)
			{
				this.includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					Console.Write((j + 1) % 10);
					Console.Write(" ");
				}
				this.includeSpace((height / 2) - i);
			}
			Console.Write("\n");
		}
		// Print bottom layer
		for (int i = height / 2; i >= 0; --i)
		{
			for (int j = 0; j < x; ++j)
			{
				this.includeSpace((height / 2) - i);
				for (int k = 0; k <= i; ++k)
				{
					Console.Write((j + 1) % 10);
					Console.Write(" ");
				}
				this.includeSpace((height / 2) - i);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5, 5);
		task.printPattern(9, 3);
	}
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
package main
import "fmt"
/*
    Go Program
    Print the diamond shape side by side
*/
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(height, x int) {
	if height % 2 == 0 {
		return
	}
	fmt.Print("\n Height : ", height, " X : ", x, " \n\n")
	// Print top layer
	for i := 0 ; i < height / 2 ; i++ {
		for j := 0 ; j < x ; j++ {
			this.includeSpace((height / 2) - i)
			for k := 0 ; k <= i ; k++ {
				fmt.Print((j + 1) % 10)
				fmt.Print(" ")
			}
			this.includeSpace((height / 2) - i)
		}
		fmt.Print("\n")
	}
	// Print bottom layer
	for i := height / 2 ; i >= 0 ; i-- {
		for j := 0 ; j < x ; j++ {
			this.includeSpace((height / 2) - i)
			for k := 0 ; k <= i ; k++ {
				fmt.Print((j + 1) % 10)
				fmt.Print(" ")
			}
			this.includeSpace((height / 2) - i)
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5, 5)
	task.printPattern(9, 3)
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
<?php
/*
    Php Program
    Print the diamond shape side by side
*/
class Pattern
{
	// This is display empty space of given length
	public	function includeSpace($n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ");
		}
	}
	public	function printPattern($height, $x)
	{
		if ($height % 2 == 0)
		{
			return;
		}
		echo("\n Height : ".$height.
			" X : ".$x.
			" \n\n");
		// Print top layer
		for ($i = 0; $i < (int)($height / 2); ++$i)
		{
			for ($j = 0; $j < $x; ++$j)
			{
				$this->includeSpace(((int)($height / 2)) - $i);
				for ($k = 0; $k <= $i; ++$k)
				{
					echo(($j + 1) % 10);
					echo(" ");
				}
				$this->includeSpace(((int)($height / 2)) - $i);
			}
			echo("\n");
		}
		// Print bottom layer
		for ($i = (int)($height / 2); $i >= 0; --$i)
		{
			for ($j = 0; $j < $x; ++$j)
			{
				$this->includeSpace(((int)($height / 2)) - $i);
				for ($k = 0; $k <= $i; ++$k)
				{
					echo(($j + 1) % 10);
					echo(" ");
				}
				$this->includeSpace(((int)($height / 2)) - $i);
			}
			echo("\n");
		}
	}
}

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

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
/*
    Node JS Program
    Print the diamond shape side by side
*/
class Pattern
{
	// This is display empty space of given length
	includeSpace(n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" ");
		}
	}
	printPattern(height, x)
	{
		if (height % 2 == 0)
		{
			return;
		}
		process.stdout.write("\n Height : " + 
                             height + " X : " + x + " \n\n");
		// Print top layer
		for (var i = 0; i < parseInt(height / 2); ++i)
		{
			for (var j = 0; j < x; ++j)
			{
				this.includeSpace((parseInt(height / 2)) - i);
				for (var k = 0; k <= i; ++k)
				{
					process.stdout.write("" + (j + 1) % 10);
					process.stdout.write(" ");
				}
				this.includeSpace((parseInt(height / 2)) - i);
			}
			process.stdout.write("\n");
		}
		// Print bottom layer
		for (var i = parseInt(height / 2); i >= 0; --i)
		{
			for (var j = 0; j < x; ++j)
			{
				this.includeSpace((parseInt(height / 2)) - i);
				for (var k = 0; k <= i; ++k)
				{
					process.stdout.write("" + (j + 1) % 10);
					process.stdout.write(" ");
				}
				this.includeSpace((parseInt(height / 2)) - i);
			}
			process.stdout.write("\n");
		}
	}
}

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

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
#    Python 3 Program
#    Print the diamond shape side by side
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, height, x) :
		if (height % 2 == 0) :
			return
		
		print("\n Height : ", height ," X : ", x ," \n")
		i = 0
		#  Print top layer
		while (i < int(height / 2)) :
			j = 0
			while (j < x) :
				self.includeSpace((int(height / 2)) - i)
				k = 0
				while (k <= i) :
					print((j + 1) % 10, end = "")
					print(" ", end = "")
					k += 1
				
				self.includeSpace((int(height / 2)) - i)
				j += 1
			
			print(end = "\n")
			i += 1
		
		i = int(height / 2)
		#  Print bottom layer
		while (i >= 0) :
			j = 0
			while (j < x) :
				self.includeSpace((int(height / 2)) - i)
				k = 0
				while (k <= i) :
					print((j + 1) % 10, end = "")
					print(" ", end = "")
					k += 1
				
				self.includeSpace((int(height / 2)) - i)
				j += 1
			
			print(end = "\n")
			i -= 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printPattern(5, 5)
	task.printPattern(9, 3)

if __name__ == "__main__": main()

Output

 Height :  5  X :  5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height :  9  X :  3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
#    Ruby Program
#    Print the diamond shape side by side
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(height, x) 
		if (height % 2 == 0) 
			return
		end

		print("\n Height : ", height ," X : ", x ," \n\n")
		i = 0
		#  Print top layer
		while (i < height / 2) 
			j = 0
			while (j < x) 
				self.includeSpace((height / 2) - i)
				k = 0
				while (k <= i) 
					print((j + 1) % 10)
					print(" ")
					k += 1
				end

				self.includeSpace((height / 2) - i)
				j += 1
			end

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

		i = height / 2
		#  Print bottom layer
		while (i >= 0) 
			j = 0
			while (j < x) 
				self.includeSpace((height / 2) - i)
				k = 0
				while (k <= i) 
					print((j + 1) % 10)
					print(" ")
					k += 1
				end

				self.includeSpace((height / 2) - i)
				j += 1
			end

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

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printPattern(5, 5)
	task.printPattern(9, 3)
end

main()

Output

 Height : 5 X : 5 

  1     2     3     4     5   
 1 1   2 2   3 3   4 4   5 5  
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 
 1 1   2 2   3 3   4 4   5 5  
  1     2     3     4     5   

 Height : 9 X : 3 

    1         2         3     
   1 1       2 2       3 3    
  1 1 1     2 2 2     3 3 3   
 1 1 1 1   2 2 2 2   3 3 3 3  
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 
 1 1 1 1   2 2 2 2   3 3 3 3  
  1 1 1     2 2 2     3 3 3   
   1 1       2 2       3 3    
    1         2         3     
/*
    Scala Program
    Print the diamond shape side by side
*/
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(height: Int, x: Int): Unit = {
		if (height % 2 == 0)
		{
			return;
		}
		print("\n Height : " + height + " X : " + x + " \n\n");
		var i: Int = 0;
		// Print top layer
		while (i < height / 2)
		{
			var j: Int = 0;
			while (j < x)
			{
				includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10);
					print(" ");
					k += 1;
				}
				includeSpace((height / 2) - i);
				j += 1;
			}
			print("\n");
			i += 1;
		}
		i = height / 2;
		// Print bottom layer
		while (i >= 0)
		{
			var j: Int = 0;
			while (j < x)
			{
				includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10);
					print(" ");
					k += 1;
				}
				includeSpace((height / 2) - i);
				j += 1;
			}
			print("\n");
			i -= 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printPattern(5, 5);
		task.printPattern(9, 3);
	}
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
/*
    Swift 4 Program
    Print the diamond shape side by side
*/
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(_ height: Int, _ x: Int)
	{
		if (height % 2 == 0)
		{
			return;
		}
		print("\n Height : ", height ," X : ", x ," \n");
		var i: Int = 0;
		// Print top layer
		while (i < height / 2)
		{
			var j: Int = 0;
			while (j < x)
			{
				self.includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10, terminator: "");
					print(" ", terminator: "");
					k += 1;
				}
				self.includeSpace((height / 2) - i);
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		i = height / 2;
		// Print bottom layer
		while (i >= 0)
		{
			var j: Int = 0;
			while (j < x)
			{
				self.includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10, terminator: "");
					print(" ", terminator: "");
					k += 1;
				}
				self.includeSpace((height / 2) - i);
				j += 1;
			}
			print(terminator: "\n");
			i -= 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5, 5);
	task.printPattern(9, 3);
}
main();

Output

 Height :  5  X :  5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height :  9  X :  3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3
/*
    Kotlin Program
    Print the diamond shape side by side
*/
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(height: Int, x: Int): Unit
	{
		if (height % 2 == 0)
		{
			return;
		}
		print("\n Height : " + height + " X : " + x + " \n\n");
		var i: Int = 0;
		// Print top layer
		while (i < height / 2)
		{
			var j: Int = 0;
			while (j < x)
			{
				this.includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10);
					print(" ");
					k += 1;
				}
				this.includeSpace((height / 2) - i);
				j += 1;
			}
			print("\n");
			i += 1;
		}
		i = height / 2;
		// Print bottom layer
		while (i >= 0)
		{
			var j: Int = 0;
			while (j < x)
			{
				this.includeSpace((height / 2) - i);
				var k: Int = 0;
				while (k <= i)
				{
					print((j + 1) % 10);
					print(" ");
					k += 1;
				}
				this.includeSpace((height / 2) - i);
				j += 1;
			}
			print("\n");
			i -= 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5, 5);
	task.printPattern(9, 3);
}

Output

 Height : 5 X : 5

  1     2     3     4     5
 1 1   2 2   3 3   4 4   5 5
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
 1 1   2 2   3 3   4 4   5 5
  1     2     3     4     5

 Height : 9 X : 3

    1         2         3
   1 1       2 2       3 3
  1 1 1     2 2 2     3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
 1 1 1 1   2 2 2 2   3 3 3 3
  1 1 1     2 2 2     3 3 3
   1 1       2 2       3 3
    1         2         3

Resultant Output Explanation

The output shows two sets of diamond shapes side by side. Each set has a different height and number of diamonds. The numbers inside the diamonds start from 1 and increase by 1 for each row. After reaching the maximum value, the numbers start again from 1. The spaces are printed to maintain the diamond shape. The top layer of each diamond is printed first, followed by the bottom layer. The output is formatted in such a way that it visually represents the diamond shape. The diamonds appear side by side due to the repetition of the printing process for the given number of diamonds.

Time Complexity Analysis

The time complexity of this program can be analyzed as follows:
- The "includeSpace" function has a time complexity of O(n), where n is the number of spaces to be printed.
- The "printPattern" function consists of two nested loops. The outer loop runs (height/2) times, and the inner loop runs x times. Therefore, the time complexity of the nested loops is O((height/2) * x).
- Overall, the time complexity of the program depends on the input values, height and x. It can be approximated as O((height/2) * x).

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.

New Comment