Print the diamond shape side by side
Here given code implementation process.
// 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
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