TutorialStudyMite

Program to print Inverted Pascal’s Triangle in C++

JJuhi Kamdar4 min read
Beginner friendly

Try it first

Right-Aligned Star Triangle

Give it a try before seeing the solution in this article.

Give it a try

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.

  1. Outer Loop (Rows): This loop iterates from 1 up to the total number of rows (n). Each iteration corresponds to printing one row of the triangle.
  2. 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. As i increases (moving down the triangle), the number of leading spaces increases. For row i, we print i-1 spaces.
  3. Inner Loop 2 (Stars): After printing the leading spaces, this loop prints the asterisks for the current row. As i increases, the number of stars decreases. For row i, we print n - i + 1 stars.
  4. Newline: After all stars for a row are printed, a newline character \n moves 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 variable n to 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 into n.
  • for (int i = 1; i <= n; ++i): This is the outer loop controlling the rows.
    • i represents the current row number, starting from 1.
    • It runs n times, 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), j loop condition 1 < 1 is false, so no spaces are printed.
    • For i=2 (second row), j runs once (j=1), printing one space.
    • For i=3 (third row), j runs twice (j=1, j=2), printing two spaces, and so on.
    • This creates the rightward indentation.
  • 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), k runs n - 1 + 1 = n times, printing n stars.
    • For i=2 (second row), k runs n - 2 + 1 = n-1 times, printing n-1 stars.
    • This decreases the number of stars with each subsequent row.
  • 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?

Was this helpful?

Your feedback shapes better tutorials for everyone.