Posted on by Kalkicode
Code Pattern

# Print diamond pattern with different layers

In this article, we will explore how to print a diamond pattern with different layers using C programming. The program will take an integer input representing the number of layers and generate the desired pattern.

## Problem Statement

The problem is to print a diamond pattern with different layers based on the input provided. Each layer of the diamond will have a unique number sequence, with the center layer containing consecutive numbers from 0 to the layer number. The layers above and below the center layer will have numbers in ascending and descending order respectively.

For example, if the input is 5, the output should be:

```     0
010
01210
0123210
012343210
0123210
01210
010
0
```

The output displays a diamond shape with five layers, where each layer contains the corresponding number sequence.

## Algorithm and Pseudocode

To solve this problem, we can use nested loops to iterate through the layers and print the numbers accordingly. Here is the algorithm and pseudocode for the solution:

1. Define a function `includeSpace` that takes an integer `n` as input and prints `n` empty spaces.
2. Define a function `printPattern` that takes an integer `n` as input.
3. Inside `printPattern`:
• Print the value of `n` to indicate the given value.
• For the top half of the diamond:
• Iterate from `i = 0` to `i < n`:
• Call `includeSpace(n - i)` to print the required spaces.
• Iterate from `j = 0` to `j <= i`:
• Print the value of `j`.
• Iterate from `k = i - 1` to `k >= 0`:
• Print the value of `k`.
• Print a new line.
• For the bottom half of the diamond:
• Iterate from `i = n - 1` to `i >= 1`:
• Call `includeSpace(n + 1 - i)` to print the required spaces.
• Iterate from `j = 0` to `j < i`:
• Print the value of `j`.
• Iterate from `k = i - 2` to `k >= 0`:
• Print the value of `k`.
• Print a new line.
4. Define the `main` function:
• Call `printPattern(5)` to test the program with an input of 5.
• Call `printPattern(8)` to test the program with an input of 8.
• Return 0 to indicate successful execution of the program.

## Code Solution

``````//  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)
{
// Test
}
}``````

#### 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()
{
// Test
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)
{
// Test
}
}``````

#### 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
}``````

#### 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()
{
// Test
}
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()
{
// Test
}
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() :
#  Test

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()
#  Test
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
}
}``````

#### 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()
{
// Test
}
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
{
// Test
}``````

#### 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``````

## Output Explanation

The given program generates the diamond pattern with different layers based on the input provided. It uses loops to iterate through the layers and print the numbers in the desired pattern.

For the input value of 5, the program generates the following output:

```     0
010
01210
0123210
012343210
0123210
01210
010
0
```

The output can be divided into two halves. The top half represents the ascending number sequence from 0 to the given number, while the bottom half represents the descending sequence. The spaces are included to create the diamond shape.

Similarly, for the input value of 8, the program generates the following output:

```        0
010
01210
0123210
012343210
01234543210
0123456543210
012345676543210
0123456543210
01234543210
012343210
0123210
01210
010
0
```

The pattern expands with more layers, creating a larger diamond shape. The ascending and descending sequences are adjusted accordingly.

## Time Complexity

The time complexity of the given code is O(n^2) because it uses nested loops to iterate through the layers and print the numbers. The outer loop iterates n times, and for each iteration, the inner loops iterate a maximum of n times. Therefore, the overall time complexity is quadratic in terms of the input size.

## 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.

Categories
Relative Post