Loading...

Introduction

Left triangle pattern printing is a classic programming exercise that helps developers strengthen their understanding of loops and pattern formation.

Understanding Left Triangle Star Patterns

Left triangle star patterns involve printing a series of stars in the shape of a left-angled triangle. The number of rows in the triangle determines the height of the pattern. For example, a left triangle star pattern with 5 rows might look like this:

*
**
***
****
*****

Algorithm

The algorithm for printing a left triangle star pattern is relatively simple. We iterate through each row, starting from the first row and moving down to the specified number of rows. In each row, we print a number of stars equal to the row number.

  1. Accept the number of rows as input.
  2. Use nested loops to iterate through each row and column.
  3. Print a star * for each column until the column number is equal to the current row number.

Code Implementation of Straight Left Triangle Star Pattern

#include <iostream>

void printLeftTrianglePattern(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            std::cout << "* ";
        }
        std::cout << "\n";
    }
}

int main() {
    int height;
    std::cout << "Enter the height of the left triangle pattern: ";
    std::cin >> height;

    printLeftTrianglePattern(height);

    return 0;
}

Output

right_triangle_pattern_printing

Explanation

  1. Inclusion of Header File: The code begins by including the <iostream> header file, enabling input and output functionalities in C++.
  2. Function Definition: The printLeftTrianglePattern function is introduced, accepting an integer parameter n representing the height of the left triangle.
  3. Nested Loop Structure: Two nested loops govern the pattern formation. The outer loop (for (int i = 0; i < n; i++)) controls the number of rows in the triangle, and the inner loop (for ( int j = 0; j <= i; j++)) manages the * characters to be printed in each column.
  4. Printing and Line Breaks:* characters are printed for each column within a row. After completing a row, a newline character (\n) is added to transition to the next line, contributing to the left triangle pattern.

Understanding the Inverted Left Triangle Star Pattern

The inverted left triangle star pattern is a visual representation where stars are arranged in the shape of an inverted left triangle.

Before we dive into the code, let's visualize the left inverted triangle star pattern with a height of 5.

*****
****
***
**
*

In this patter, the maximum number of stars is in the first row, and the count decreases as we move downwards, but the stars are left-aligned.

Algorithm

TO generate an inverted left triangle star pattern, we need to use nested loops. The outer loop controls the number of rows, and the inner loop manages the printing of stars in each row. Let's break down it step by step:

  1. Accept the number of rows from the user.
  2. Use an outer loop to iterate through each row.
  3. Use an inner loop to print stars in each row, decrementing the count with each iteration.
  4. Move to the next line after completing each row.

Let's implement it:

Code Implementation of Inverted Left Triangle Star Pattern

#include <iostream>

int main() {
    int height = 5;

    for (int i = 1; i <= height; ++i) {
        // Print stars in the left inverted triangle
        for (int j = height; j >= i; --j) {
            std::cout << "*";
        }

        // Move to the next line after each row
        std::cout << std::endl;
    }

    return 0;
}

or 
#include <iostream>

int main() {
    int rows;

    // Accept the number of rows from the user
    std::cout << "Enter the number of rows: ";
    std::cin >> rows;

    // Outer loop for rows
    for (int i = rows; i >= 1; --i) {
        // Inner loop for printing stars
        for (int j = 1; j <= i; ++j) {
            std::cout << "* ";
        }
        // Move to the next line after each row
        std::cout << std::endl;
    }

    return 0;
}

Output:

left_triangle_pattern_printing
Explanation

  1. The outer loop (for (int i = 1; i <= height; ++i)) iterates through each row in an increasing order, starting from 1 and going up to the maximum height.
  2. The inner loop (for (int j = height; j >= i; --j)) prints the stars in the left inverted triangle pattern. The loops starts with the maximum number of stars and decreases as we move to the left.
  3. The std::endl is used to move to the next line after printing each row.

Complexity Analysis

Time Complexity: The time complexity of this pattern algorithm is O(n^2), where n is the height of the triangle. This is because there are two nested loops, with the outer loop iterating n times and the inner loop having a maximum of n iterations. The total number of iterations is proportional to the square of the input size.

Space Complexity: The space complexity is O(1), as the program uses a constant amount of additional space, regardless of the input size.