Program to print Inverted Pascal’s Triangle in C++
Try it first
Right-Aligned Star Triangle
Give it a try before seeing the solution in this article.
Track completion, mastery, and revision.
This article explores how to generate a specific inverted triangular pattern using C++. While the pattern is often referred to as an "Inverted Pascal's Triangle," it's important to note that the provided code generates a triangle of asterisks, not the numerical coefficients typically associated with Pascal's Triangle. We'll focus on understanding the logic behind creating this inverted star pattern.
Understanding the Inverted Star Triangle Pattern
The pattern we aim to print looks like this for n=5 rows:
* * * * *
* * * *
* * *
* *
*
Notice how each subsequent row has one fewer star and is indented further to the right.
Algorithm for Printing the Inverted Star Triangle
To achieve this inverted star pattern, we'll use nested loops. The core idea is to control the number of stars and the leading spaces for each row.
- Outer Loop (Rows): This loop iterates from
1up to the total number of rows (n). Each iteration corresponds to printing one row of the triangle. - Inner Loop 1 (Leading Spaces): Before printing stars for a given row
i, we need to print a certain number of leading spaces to create the indentation. Asiincreases (moving down the triangle), the number of leading spaces increases. For rowi, we printi-1spaces. - Inner Loop 2 (Stars): After printing the leading spaces, this loop prints the asterisks for the current row. As
iincreases, the number of stars decreases. For rowi, we printn - i + 1stars. - Newline: After all stars for a row are printed, a newline character
\nmoves the cursor to the next line for the subsequent row.
C++ Code Implementation
Here's the C++ program that implements the algorithm described above:
#include <iostream>
int main() {
int n;
std::cout << "Enter number of rows: ";
std::cin >> n;
std::cout << "\n"; // Add a newline for better output formatting
// Outer loop for rows
for (int i = 1; i <= n; ++i) {
// Inner loop 1: Print leading spaces
// For row i, print i-1 spaces
for (int j = 1; j < i; ++j) {
std::cout << " ";
}
// Inner loop 2: Print stars
// For row i, print n - i + 1 stars
for (int k = 1; k <= n - i + 1; ++k) {
std::cout << "* "; // Print star followed by a space
}
std::cout << "\n"; // Move to the next line after each row
}
return 0;
}
Code Explanation
Let's break down the key parts of the code:
int n;: Declares an integer variablento store the user-specified number of rows.std::cout << "Enter number of rows: "; std::cin >> n;: Prompts the user to enter the number of rows and reads the input inton.for (int i = 1; i <= n; ++i): This is the outer loop controlling the rows.irepresents the current row number, starting from1.- It runs
ntimes, once for each row.
for (int j = 1; j < i; ++j): This is the first inner loop responsible for printing leading spaces.- For
i=1(first row),jloop condition1 < 1is false, so no spaces are printed. - For
i=2(second row),jruns once (j=1), printing one space. - For
i=3(third row),jruns twice (j=1, j=2), printing two spaces, and so on. - This creates the rightward indentation.
- For
for (int k = 1; k <= n - i + 1; ++k): This is the second inner loop responsible for printing the stars.- For
i=1(first row),krunsn - 1 + 1 = ntimes, printingnstars. - For
i=2(second row),krunsn - 2 + 1 = n-1times, printingn-1stars. - This decreases the number of stars with each subsequent row.
- For
std::cout << "* ";: Prints an asterisk followed by a space. This ensures separation between stars.std::cout << "\n";: After all spaces and stars for a row are printed, this moves the cursor to the next line, preparing for the next row.
Example Output
When you compile and run the program, and enter 5 for the number of rows, you will get the following output:
Enter number of rows: 5
* * * * *
* * * *
* * *
* *
*
Note on True Inverted Pascal's Triangle (Numbers)
It's important to clarify that a true Inverted Pascal's Triangle, in the context of numbers, would display the binomial coefficients in an inverted fashion. Generating such a triangle would involve calculating nCr (n choose r) values for each position, similar to a standard Pascal's Triangle, but arranging them to form an inverted shape. The code provided here focuses solely on the visual pattern of stars, which is a common programming exercise.
Finished reading?