Tech Master 07

Posted on

# Implementing diamond pattern printing programs in different programming languages

Diamond pattern programs are very common and every one of us might have learned these programs at the start of our journey. Programmers who have recently started learning, frequently practice implementing programs that print diamond patterns better to grasp loops, conditions, and other control statements.

This type of program outputs a diamond shape and it can be written in many languages like C, C++, Java, Python, and JavaScript. The basic logic behind the programs remains the same, which is to use nested loops to iterate through the rows and columns of the diamond pattern and print out the desired pattern as mentioned.

This process involves identifying the pattern size, creating loops to iterate through the rows and columns, and using conditional statements to decide when to print the symbol. This type of program helps us to build programming logic.

## Scope of the article

In this article, we will read about a small introduction to the diamond pattern printing program and its purpose as a programming exercise for logic building.

In this article mainly we will see all the sample code snippets and explanations of how the diamond pattern printing program can be implemented in popular programming languages like C, C++, Java, Python, and JavaScript.

## Introduction

As we know the implementation of the diamond pattern is a very common program that is solved by beginners to improve their problem-solving and improve their logic-building abilities and also learn more about the programming language they are using.

This type of program involves printing out a diamond shape using a particular symbol, such as asterisks or hashtags, or any other symbol or figure. Many programming languages, including C++, Java, Python, and JavaScript, can be used to implement this type of program.

The process of implementing a diamond pattern printing program may differ depending on the programming language used, but the basic idea remains the same.

You should have a good concept of loops and nested loops to solve these types of programs.

Hence it is recommended that beginners should solve these types of pattern printing programs as it is an excellent exercise for programmers to develop their skills and learn more about the programming language they are using.

Now let us read about how to implement these types of programs in different programming languages:

## 1. Implementing diamond pattern printing program in C programming language

Now let us see the Implementation of the diamond pattern printing program in C programming language:

# include

```int main() { int n, i, j, space; print("Enter the number of rows: "); scarf("%d", &n); space = n - 1; for (i = 0; i < n; i++) { for (j = 0; j < space; j++) { print(" "); } for (j = 0; j <= i; j++) { printf("* "); } printf("\n"); space--; } space = 0; for (i = n; i > 0; i--) { for (j = 0; j < space; j++) { print(" "); } for (j = 0; j < i; j++) { printf("* "); } printf("\n"); space++; } return 0; }```

In the above program, we have taken input n which represents the number of rows of the diamond pattern to be printed. We have also initialized the variables i, j, and space.

There are two halves in this code the upper half and the lower half print the triangle in reverse order forming the diamond shape. In the first half the loop iterates I from 0 to n-1. At each iteration, it prints the space number of spaces before printing the i+1 number of * symbols.

In the second half, it iterates I from n to 1 because we are printing the triangle in reverse order. At each iteration, it prints space number of spaces before printing I several * symbols. Then, it moves to the following line and increases the value of freedom by 1 to adjust for the decreasing number of * symbols.

Run the code and it prints a diamond shape like figure.

Now let us read about how to implement a diamond pattern program in C++ programming language:

## 2. Implementing diamond pattern printing program in C++ programming language

Now let us see the Implementation of the diamond pattern printing program in C++ programming language:

`#include

using namespace std;

int main() {
int n, i, j, space = 1;

``````cout << "Enter the number of rows: ";
can >> n;

space = n - 1;

for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
cout << " ";
}

space--;

for (i = 1; i <= 2 * j - 1; i++) {
cout << "*";
}

cout << endl;
}

space = 1;

for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= space; i++) {
cout << " ";
}

space++;

for (i = 1; i <= 2 * (n - j) - 1; i++) {
cout << "*";
}

cout << endl;
}

return 0;
``````

}`

In the above program we have taken input n which represents the number of rows of the diamond pattern to be printed. We have also initialized the variables i, j, and space.

There are two halves in this code the upper halves and the lower half print the triangle in reverse order forming the diamond shape. In the first half the loop iterates i from 0 to n-1. At each iteration, it prints space number of spaces before printing i+1 number of * symbols.

In the second half it iterates i from n to 1 because we are printing the triangle in reverse order. At each iteration, it prints space number of spaces before printing i number of * symbols. Then, it moves to the next line and increases the value of space by 1 to adjust for the decreasing number of * symbols.

Run the code and it will print an diamond shape like figure.

Now let us read about how to implement diamond pattern program in Java programming language:

## 3.Implementing diamond pattern printing program in Java programming language

Now let us see the Implementation of diamond pattern printing program in Java programming language:

`import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int n, i, j, space = 1;

``````    Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
n = scanner.nextInt();

space = n - 1;

for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}

space--;

for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}

System.out.println();
}

space = 1;

for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}

space++;

for (i = 1; i <= 2 * (n - j) - 1; i++) {
System.out.print("*");
}

System.out.println();
}
}
``````

}
`

In the above program, we have taken input n which represents the number of rows of the diamond pattern to be printed. We have also initialized the variables i, j, and space.

There are two halves in this code the upper half and the lower half print the triangle in reverse order forming the diamond shape. In the first half the loop iterates I from 0 to n-1. At each iteration, it prints the space number of spaces before printing the i+1 number of * symbols.

In the second half, it iterates I from n to 1 because we are printing the triangle in reverse order. At each iteration, it prints space number of spaces before printing i several * symbols. Then, it moves to the following line and increases the value of latitude by 1 to adjust for the decreasing number of * symbols.

Run the code and it will print a diamond shape like figure.
Now let us read about how to implement a diamond pattern program in the python programming language:

## 4. Implementing diamond pattern printing program in Python programming language

Now let us see the Implementation of the diamond pattern printing program in Python programming language:

`n = int(input("Enter the number of rows: "))
space = n - 1

for j in range(1, n+1):
for i in range(1, space+1):
print(" ", end="")
space -= 1

``````for i in range(1, 2*j):
print("*", end="")
print()
``````

space = 1

for j in range(1, n):
for i in range(1, space+1):
print(" ", end="")
space += 1

``````for i in range(1, 2*(n-j)):
print("*", end="")
print()
``````

`

In the above program, we have taken input n which represents the number of rows of the diamond pattern to be printed. We have also initialized the variables i, j, and space. This can also be achieved by using recursion.

There are two halves in this code the upper half and the lower half print the triangle in reverse order forming the diamond shape. In the first half the loop iterates I from 0 to n-1. At each iteration, it prints the space number of spaces before printing the i+1 number of * symbols.

Run the code and it will print a diamond shape like figure.

Now let us read about how to implement a diamond pattern program in javascript language:

## 5. Implementing diamond pattern printing program in Javascript language

Now let us see the Implementation of the diamond pattern printing program in Javascript language:

`let n, i, j, space = 1;

n = parseInt(prompt("Enter the number of rows:"));

space = n - 1;

for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
document.write("Â ");
}

space--;

for (i = 1; i <= 2 * j - 1; i++) {
document.write("*");
}

document.write("
");
}

space = 1;

for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= space; i++) {
document.write("Â ");
}

space++;

for (i = 1; i <= 2 * (n - j) - 1; i++) {
document.write("*");
}

document.write("
");
}
`

In the above program, we have taken input n which represents the number of rows of the diamond pattern to be printed. We have also initialized the variables i, j, and space.

There are two halves in this code the upper half and the lower half print the triangle in reverse order forming the diamond shape. In the first half the loop iterates I from 0 to n-1. At each iteration, it prints the space number of spaces before printing the i+1 number of * symbols.

In the second half, it iterates I from n to 1 because we are printing the triangle in reverse order. At each iteration, it prints space number of spaces before printing i a number of * symbols. Then, it moves to the following line and increases the value of latitude by 1 to adjust for the decreasing number of * symbols.

Run the code and it will print a diamond shape like figure. Since we have used the document. write a method to output the result to the web page so it will not print the result in the console.

Now let us read about the time complexity of each of the programs in various languages:

## Time complexity

The general time complexity for such a program is O(n^2), where n is the number of rows or columns in the diamond because we are using nested loops.

## Conclusion

Implementing diamond pattern printing programs in different programming languages is possible, and the time complexity of such programs is generally O(n^2), where n is the size of the diamond.

The approach used to implement the program will affect its efficiency and readability.