C++ Online Compiler
Example: Number Diamond Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Number Diamond Pattern #include <iostream> using namespace std; int main() { int size; cout << "Enter the size of the diamond (e.g., 4 for a 7-row diamond): "; cin >> size; // Upper half of the diamond (including the widest row) for (int i = 1; i <= size; i++) { // Step 1: Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Step 2: Print increasing numbers for (int k = 1; k <= i; k++) { cout << k; } // Step 3: Print decreasing numbers for (int k = i - 1; k >= 1; k--) { cout << k; } cout << endl; // Move to the next line } // Lower half of the diamond (excluding the widest row) for (int i = size - 1; i >= 1; i--) { // Step 1: Print leading spaces for (int j = 1; j <= size - i; j++) { cout << " "; } // Step 2: Print increasing numbers for (int k = 1; k <= i; k++) { cout << k; } // Step 3: Print decreasing numbers for (int k = i - 1; k >= 1; k--) { cout << k; } cout << endl; // Move to the next line } return 0; }
Output
Clear
ADVERTISEMENTS