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 inputn = 5
, the output of both codes will be:
*
***
*****
*******
*********
Explanation of the Code:
-
- We prompt the user to enter the number of rows in the pyramid pattern and store the value in the variable
n
.
- We prompt the user to enter the number of rows in the pyramid pattern and store the value in the variable
-
- The outer loop, controlled by the variable
i
, runs from 0 ton-1
, iterating over the rows of the pyramid pattern.
- The outer loop, controlled by the variable
-
- 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 byn - i - 1
.
- Inside the outer loop, the first inner loop, controlled by the variable
-
- The second inner loop, controlled by the variable
k
, prints the ‘‘ symbol to form the pyramid pattern. The number of ‘‘ symbols is determined by2 * i + 1
.
- The second inner loop, controlled by the variable
-
- After both inner loops complete for each row, we print a newline character to move to the next row.
-
- 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:
-
- In the first inner loop, the condition
j < n - i - 1
is changed toj < i
to print spaces in each row.
- In the first inner loop, the condition
-
- In the second inner loop, the condition
k < 2 * i + 1
is changed tok < 2 * (n - i) - 1
to adjust the number of ‘*’ symbols printed in each row.
- In the second inner loop, the condition
-
- The adjustment in the second inner loop ensures that the number of ‘*’ symbols decreases as the rows progress, creating the opposite pyramid triangle shape.
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”