Open In App

C++ Program To Print Left Half Pyramid Pattern

Last Updated : 23 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Share
23 Likes
Like
Report

Here, we will build a C++ program to print the left half of pyramid pattern using 2 approaches i.e.

  1. Using for loop
  2. Using while loop

1. Using for loop

Input: 

rows = 5

Output:

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

First, for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values will be changed according to the first for loop.  If j is greater than i then it will print the output otherwise print the space.


Output
     *
    **
   ***
  ****
 *****

Time complexity: O(n2

Here n is number of rows.

Auxiliary Space: O(1)

As constant extra space is used.

2. Using while loop

Input: 

rows = 5

Output:

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

The while loops check the condition until the condition is false. If the condition is true then it enters into the loop and executes the statements. 


Output
        * 
      * * 
    * * * 
  * * * * 
* * * * * 

Time complexity: O(n2) where n is number of rows

Space complexity: O(1)


Next Article
Practice Tags :

Similar Reads