# Printing the Diamond Shape Pattern:

Diamond shape patterns are fascinating and visually appealing. They provide an excellent opportunity to strengthen programming skills by utilizing nested loops effectively. In this post, we will explore how to create a diamond shape pattern using nested loops in both C++ and Python. We will break down the process step by step and provide code examples to help you understand the logic behind it. Lets start now.

## Printing the Diamond Shape Pattern:

To print the diamond shape pattern, we will use nested loops. The outer loop controls the rows, and the inner loops control the spaces and symbols within each row. The value of ‘n’ determines the number of iterations for each loop.

### C++ Code:

``````#include <iostream>

int main() {
int n;
std::cout << "Enter the number of rows in the diamond shape pattern: ";
std::cin >> n;

int i, j, space;

// Upper half of the diamond
for (i = 0; i < n; i++) {
for (space = 0; space < n - i - 1; space++) {
std::cout << " ";
}
for (j = 0; j < 2 * i + 1; j++) {
std::cout << "*";
}
std::cout << std::endl;
}

// Lower half of the diamond
for (i = n - 2; i >= 0; i--) {
for (space = 0; space < n - i - 1; space++) {
std::cout << " ";
}
for (j = 0; j < 2 * i + 1; j++) {
std::cout << "*";
}
std::cout << std::endl;
}

return 0;
}``````

### Python Code:

``````n = int(input("Enter the number of rows in the diamond shape pattern: "))

# Upper half of the diamond
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))

# Lower half of the diamond
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))``````

### Output:

For the input `n = 5`, the output of both codes will be:

``````    *
***
*****
*******
*********
*******
*****
***
*``````

## Explanation of the Code:

1. We prompt the user to enter the number of rows in the diamond shape pattern and store the value in the variable `n`.
2. First set of loops prints the upper half of the diamond. The outer loop, controlled by the variable `i`, runs from 0 to `n-1`, iterating over the rows of the upper half.
3. Inside the outer loop, the first inner loop prints the required number of spaces before each row to create the triangular shape. The number of spaces is determined by `n - i - 1`.
4. The second inner loop prints the ‘‘ symbol to form the diamond shape. The number of ‘‘ symbols is determined by `2 * i + 1`.
5. After completing both inner loops, the code prints a newline character to move to the next row.
6. The lower half of the diamond is printed using a similar set of loops. However, the outer loop starts from `n - 2` and decrements by 1 until it reaches 0, iterating over the rows of the lower half.
7. Repeat the process for each row until the complete diamond shape pattern is created.

## Customizing the Diamond Shape Pattern:

To customize the size of the diamond shape pattern, you can take the input ‘n’ from the user, similar to the previous examples. By modifying the value of ‘n’, you can increase or decrease the size of the diamond pattern accordingly.

Your Task is to Print This Diamond Pattern, Your Hint is Ascii code of A is 65, Ascii code of B is 66 and so on… you will have to play with value of i and ascii code and use char () method to convert int value to ascii character.

``````     A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
DDDDDDD
CCCCC
BBB
A
``````

In conclusion, creating a diamond shape pattern using nested loops is a challenging yet rewarding exercise for improving programming skills. By understanding the logic and applying the provided code examples in C++ and Python, you can easily print impressive diamond shape patterns. Enjoy experimenting with different sizes and customizations to enhance your coding abilities. Happy coding!

Read our another Post : Why Pattern Printing is Important?