C++ Online Compiler
Example: Rhombus Pattern Printer in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Rhombus Pattern Printer #include <iostream> using namespace std; int main() { // Step 1: Declare a variable for the rhombus side length int side_length; // Step 2: Prompt the user to enter the side length cout << "Enter the side length of the rhombus: "; cin >> side_length; // Step 3: Outer loop for rows // This loop iterates from the first row (i=1) up to the specified side_length. for (int i = 1; i <= side_length; ++i) { // Step 4: Inner loop to print leading spaces // The number of spaces decreases with each row to create the diagonal shift. // For the first row (i=1), side_length - 1 spaces are printed. // For the last row (i=side_length), 0 spaces are printed. for (int j = 1; j <= side_length - i; ++j) { cout << " "; } // Step 5: Inner loop to print stars // The number of stars remains constant for a parallelogram-style rhombus. // It prints 'side_length' number of stars in each row. for (int k = 1; k <= side_length; ++k) { cout << "*"; } // Step 6: Move to the next line after each row is complete cout << endl; } return 0; }
Output
Clear
ADVERTISEMENTS