Pyramid pattren program in C++

#include <iostream>

int main() {
    int rows;

    std::cout << "Enter the number of rows: ";
    std::cin >> rows;

    for (int i = 1; i <= rows; ++i) {
        for (int j = 1; j <= i; ++j) {
            std::cout << "* ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Explanation:

The above code is a C++ program to print a pyramid pattern using asterisks. It prompts the user to enter the number of rows for the pyramid and then prints the pattern accordingly.

  1. The #include <iostream> statement includes the necessary header file for input/output operations.
  2. The int main() function is the entry point of the program.
  3. The int rows; declaration creates a variable to store the number of rows entered by the user.
  4. The std::cout << "Enter the number of rows: "; statement displays a prompt asking the user to enter the number of rows.
  5. The std::cin >> rows; statement reads the user input and stores it in the rows variable.
  6. The outer for loop is used to iterate over each row of the pyramid. It starts from 1 and continues until i reaches the number of rows entered by the user.
  7. Inside the outer loop, there is an inner for loop. This loop is used to print the asterisks for each row. It starts from 1 and continues until j reaches the current row number i.
  8. Inside the inner loop, the statement std::cout << "* "; is used to print an asterisk followed by a space.
  9. After printing the asterisks for a row, the statement std::cout << std::endl; is used to move to the next line.
  10. After the inner loop completes for a row, the outer loop moves to the next row and repeats the process.
  11. Finally, the return 0; statement indicates the successful termination of the program.