C++ Online Compiler
Example: Number Pyramid Pattern in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Number Pyramid Pattern #include <iostream> #include <iomanip> // For setw using namespace std; int main() { // Step 1: Declare variable for number of rows int rows; // Step 2: Prompt user for input cout << "Enter the number of rows for the number pyramid: "; cin >> rows; // Step 3: Outer loop for rows for (int i = 1; i <= rows; ++i) { // Step 4: Inner loop to print leading spaces for (int j = 1; j <= rows - i; ++j) { cout << " "; // Two spaces for alignment with numbers } // Step 5: Inner loop to print numbers in increasing order for (int k = 1; k <= i; ++k) { cout << k << " "; } // Step 6: Inner loop to print numbers in decreasing order for (int k = i - 1; k >= 1; --k) { cout << k << " "; } // Step 7: Move to the next line after each row cout << endl; } return 0; }
Output
Clear
ADVERTISEMENTS