C++ Online Compiler
Example: Hollow Diamond Pattern in C++ using For loop
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Hollow Diamond Pattern in C++ using For loop #include <iostream> using namespace std; int hollowDiamond(int x); int main() { int x; // x - denotes the size of the hollow diamond cout << "-----Enter the size of the hollow diamond, it should be even-----\n"; cin >> x; // Size of the hollow diamond should be even number if (x % 2 == 1) x++; // This will print the hollow diamond pattern hollowDiamond(x); return 0; } int hollowDiamond(int size) { int i, j, diff; diff = size / 2; cout << "\n-----The hollow diamond pattern is-----\n\n"; // This will print the first half diamond for (i = 1; i <= diff; i++) { cout << "\t"; for (j = 1; j <= diff - i; j++) { cout << " "; } if (i == 1) { cout << "*"; } else { cout << "*"; for (j = 1; j <= 2 *i - 3; j++) { cout << " "; } cout << "*"; } cout << "\n"; } // This will print the last half diamond for (i = diff + 1; i < size; i++) { cout << "\t"; for (j = 1; j <= i - diff; j++) { cout << " "; } if (i == size - 1) { cout << "*"; } else { cout << "*"; for (j = 1; j <= 2 *(size - i) - 3; j++) { cout << " "; } cout << "*"; } cout << "\n"; } }
16
Output
Clear
ADVERTISEMENTS