0% found this document useful (0 votes)
2 views

pyramidpatternnestedloop

The program prompts the user to enter the number of rows for a pyramid and then uses nested loops to print a symmetrical pyramid pattern using asterisks. The outer loop iterates through each row, while the inner loop determines whether to print an asterisk or a space based on the current column's position. The pyramid's shape is maintained by calculating the appropriate column indices for printing asterisks.

Uploaded by

yzay46486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

pyramidpatternnestedloop

The program prompts the user to enter the number of rows for a pyramid and then uses nested loops to print a symmetrical pyramid pattern using asterisks. The outer loop iterates through each row, while the inner loop determines whether to print an asterisk or a space based on the current column's position. The pyramid's shape is maintained by calculating the appropriate column indices for printing asterisks.

Uploaded by

yzay46486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

int main() {

int n;

// Asking the user for the number of rows

cout << "Enter the number of rows for the pyramid: ";

cin >> n;

for (int row = 1; row <= n; row++) {

for (int column = 1; column <= 2 * n - 1; column++) {

if (column >= n - (row - 1) && column <= n + (row - 1)) {

cout << "*";

} else {

cout << " ";

cout << endl;

return 0;

 The program asks the user to input the number of rows for the pyramid.
 The outer loop runs from 1 to n, representing each row of the pyramid.
 The inner loop runs from 1 to 2 * n - 1, representing the number of columns in each row.
 The condition column >= n - (row - 1) && column <= n + (row - 1) checks if the
current column should print a * or a space.
 The pyramid is symmetrical, so the condition ensures that * is printed in the middle section,
while spaces are printed on either side.

You might also like