C++ Online Compiler
Example: Centered Number Pyramid in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Centered Number Pyramid #include <iostream> using namespace std; int main() { int rows = 5; // Number of rows for the pyramid // Outer loop for rows for (int i = 1; i <= rows; ++i) { // Loop to print leading spaces for centering // The number of spaces decreases as 'i' increases for (int space = 1; space <= rows - i; ++space) { cout << " "; } // Loop to print increasing numbers (1 to i) for (int j = 1; j <= i; ++j) { cout << j; } // Loop to print decreasing numbers (i-1 down to 1) // This creates the symmetrical part of the pyramid for (int k = i - 1; k >= 1; --k) { cout << k; } cout << endl; // Move to the next line } return 0; }
Output
Clear
ADVERTISEMENTS