Parallelogram Star Pattern Using C++



When we start learning to code, practicing star patterns is one of the best ways to improve logic building. One of the engaging patterns is the parallelogram pattern. In this article, we are going to learn how to print a Parallelogram Star Pattern using C++.

What is Parallelogram Pattern?

A parallelogram pattern is a geometrical arrangement of stars in a parallelogram shape. The stars are printed in such a manner that if we draw a closed figure on the boundary of the printed pattern, it will resemble a parallelogram. Each row is shifted with space, which creates a diagonal slant.

The parallelogram pattern looks like this:

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

Using Row-Shifted Space Reduction Approach

We will first enter the number of rows and columns we want to print.

Then we add spaces before stars. The spaces decrease as we move to the next row, creating a slant pattern. In a parallelogram, the stars are aligned diagonally, but the rows are shifted to form a slanted shape.

Now, we print the same number of stars on each row.

Steps for Implementation

  • Input the number of rows and columns from the user.
  • Use nested loops to print the parallelogram pattern.
  • The outer loop iterates through the rows.
  • The first inner loop prints decreasing spaces for each row to create the slant.
  • The second inner loop prints stars for the given number of columns.
  • Move to the next line after each row and continue until the specified number of rows is reached.

C++ Implementation

#include<bits/stdc++.h>
using namespace std;

int main() {
    int rows, cols;
    cout << "Enter the number of rows: ";
    cin >> rows;
    cout << "Enter the number of columns: ";
    cin >> cols;

    for (int i = 0; i < rows; i++) {
        // Print spaces
        for (int j = 0; j < rows - i - 1; j++) {
            cout << " ";
        }
        // Print stars
        for (int j = 0; j < cols; j++) {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Output:

Enter the number of rows: 5
Enter the number of columns: 5
    *****
   *****
  *****
 *****
*****

Time Complexity: O(n2), as we are using a nested loop.

Space Complexity: O(1), constant space.

Using String Concatenation Approach

We use the string method to print the parallelogram pattern. A nested loop is used, where the outer loop is for the number of rows. The first inner loop initializes a string object with spaces (rows - i - 1) for the slant. The second inner loop is for a fixed number of stars. We concatenate the spaces and stars, print the result, and repeat for each row.

Steps for Implementation

  • Take input for the number of rows and columns.
  • Use a nested loop where the outer loop iterates through each row.
  • Create a string of spaces for the current row, decreasing as we move to the next row.
  • Create a string of stars for the current row.
  • Print the concatenated string of spaces and stars.

C++ Implementation

#include<bits/stdc++.h>
using namespace std;

int main() {
    int rows = 5;
    int cols = 5;

    for (int i = 0; i < rows; i++) {
        string spaces(rows - i - 1, ' ');
        string stars(cols, '*');
        cout << spaces + stars << endl;
    }
    return 0;
}

Output:

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

Time Complexity: O(n * m)

Space Complexity: O(n + m)

Updated on: 2024-11-28T11:14:45+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements