Diamond Pattern
Diamond Pattern in C++ (3 Programs With Output)
IntermediateTopic: Advanced Pattern Programs
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;
}Output
Enter number of rows (half): 5
*
***
*****
*******
*********
*******
*****
***
*Understanding Diamond Pattern
This program demonstrates 3 different diamond patterns: solid diamond, hollow diamond, and number diamond. The diamond is created by combining an upper pyramid and an inverted lower pyramid.
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.