Print Pyramid Pattern

In our last lesson we learned about Printing Right Triangle Pattern. Today we will learn the printing of pyramid pattern. To print the pyramid pattern, we will use nested loops. The outer loop controls the rows, and the inner loop controls the columns within each row. The number of iterations for each loop will be determined by the value of ‘n’.

C++ Code:

#include <iostream>

int main() {
    int n;
    std::cout << "Enter the number of rows in the pyramid pattern: ";
    std::cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            std::cout << " ";
        }
        for (int k = 0; k < 2 * i + 1; k++) {
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    return 0;
}

Python Code:

n = int(input("Enter the number of rows in the pyramid pattern: "))

for i in range(n):
    for j in range(n - i - 1):
        print(" ", end="")
    for k in range(2 * i + 1):
        print("*", end="")
    print()

Output:

For the input n = 5, the output of both codes will be:
    *
   ***
  *****
 *******
*********

Explanation of the Code:

    1. We prompt the user to enter the number of rows in the pyramid pattern and store the value in the variable n.
    1. The outer loop, controlled by the variable i, runs from 0 to n-1, iterating over the rows of the pyramid pattern.
    1. Inside the outer loop, the first inner loop, controlled by the variable j, prints the spaces before each row to create the pyramid shape. The number of spaces is determined by n - i - 1.
    1. The second inner loop, controlled by the variable k, prints the ‘‘ symbol to form the pyramid pattern. The number of ‘‘ symbols is determined by 2 * i + 1.
    1. After both inner loops complete for each row, we print a newline character to move to the next row.
    1. This process repeats for each row until the pyramid pattern is complete.

Modifying the Pattern:

To create variations in the pyramid pattern, we can modify the characters used and adjust the number of spaces or symbols printed in each row. Customizing the Pyramid Pattern: To customize the number of rows in the pyramid pattern, you can take the input ‘n’ from the user, similar to the previous examples. By modifying the value of ‘n’, you can increase or decrease the size of the pyramid accordingly.

Opposite Pyramid:

To print the opposite pyramid triangle pattern, you need to modify the conditions in the C++ code provided. Specifically, you need to adjust the conditions inside the nested loops responsible for printing spaces and symbols. Here’s the modified C++ code to print the opposite pyramid triangle pattern:
#include <iostream>

int main() {
    int n;
    std::cout << "Enter the number of rows in the opposite pyramid triangle pattern: ";
    std::cin >> n;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            std::cout << " ";
        }
        for (int k = 0; k < 2 * (n - i) - 1; k++) {
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    return 0;
}

Explanation of the Modification:

    1. In the first inner loop, the condition j < n - i - 1 is changed to j < i to print spaces in each row.
    1. In the second inner loop, the condition k < 2 * i + 1 is changed to k < 2 * (n - i) - 1 to adjust the number of ‘*’ symbols printed in each row.
    1. The adjustment in the second inner loop ensures that the number of ‘*’ symbols decreases as the rows progress, creating the opposite pyramid triangle shape.
By running the modified code with the desired number of rows, you will be able to print the opposite pyramid triangle pattern.

Modify the Python code yourself based on above Explanation of Modification

I hope this article helps you understand how to print a pyramid pattern using nested loops in both C++ and Python. By experimenting with different modifications, you can create various pyramid patterns and further strengthen your coding skills. Happy coding!

1 thought on “Print Pyramid Pattern”

Leave a Comment