Diamond Pattern
Diamond Pattern in C++ (3 Programs With Output)
C++ Diamond Pattern Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows (half): ";
cin >> rows;
// Upper part of diamond
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
// Lower part of diamond
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}Enter number of rows (half): 5
*
***
*****
*******
*********
*******
*****
***
*Understanding Diamond Pattern
This program teaches you how to print a diamond pattern in C++ using nested loops. A diamond pattern is created by combining an upper pyramid (increasing stars) and a lower inverted pyramid (decreasing stars). This is a classic pattern printing problem that demonstrates advanced loop control and pattern recognition.
---
1. What This Program Does
The program prints a diamond pattern based on the number of rows entered. For example, with 5 rows (half-diamond), it creates:
The diamond is symmetric - the upper and lower halves mirror each other.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Diamond Pattern
Structure
:
A diamond consists of two parts:
Key Insight
:
Example
(input = 5):
---
4. Declaring Variables
The program declares:
int rows;
---
5. Taking Input From the User
The program asks:
cin >> rows;
This represents half the diamond (upper part).
---
cout << "Enter number of rows (half): ";6. Upper Part of Diamond
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
cout << " ";How it works
:
Output
(for rows = 5, upper part):
*
***
*
***
*
---
7. Lower Part of Diamond
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
cout << " ";How it works
:
Output
(for rows = 5, lower part):
***
*
***
*
---
8. Complete Diamond Pattern
Combining both parts creates the full diamond:
Upper Part
(rows 1-5):
*
***
*
***
*
Lower Part
(rows 6-9):
***
*
***
*
Complete Diamond
:
*
***
*
***
*
***
*
***
*
---
9. Understanding the Algorithm
Why rows - 1 in lower loop?
:
Formula 2*i - 1
:
Spacing Logic
:
---
10. Other Patterns (Mentioned but not shown in code)
Hollow Diamond
:
Number Diamond
:
---
11. When to Use Diamond Patterns
Educational Purposes
:
Interview Preparation
:
Visual Programming
:
---
12. Important Considerations
Row Calculation
:
Loop Boundaries
:
Spacing
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning advanced pattern printing, understanding symmetric patterns, and preparing for complex pattern problems in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Diamond Pattern
This C++ program is part of the "Advanced Pattern Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.