Open In App

C Program For Printing 180 Degree Rotation of Simple Half Left Pyramid

Last Updated : 10 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

We can print the 180-degree rotation of simple half-left pyramid using for and while loop as follows:

Input:

rows = 5

Output:

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

Approach 1:

Using for loop:

Explanation: 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. 

C




// C program to print 180 degree rotation
// of a simple half left pyramid pattern
#include <stdio.h>
 
int main()
{
 
    int rows = 5;
 
    // first for loop is used to identify number of rows
    for (int i = rows; i > 0; i--) {
       
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (int j = 0; j <= rows; j++) {
           
            // if j is greater than i then it will print
            // the output otherwise print the space
            if (j >= i) {
                printf("*");
            }
            else {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}


Output

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

Approach 2:

Using while loop

Explanation: The while loops check the condition until the condition is false. If condition is true then enters in to loop and execute the statements. 

C




// C program to print 180 degree rotation
// of a simple half left pyramid pattern
// using while loop
 
#include <stdio.h>
 
int main()
{
 
    int i = 0, j = 0, sp = 0;
    int rows = 5;
   
    // while loop check the condition until the given
    // condition is false if it is true then enteres in to
    // the loop
 
    while (i < rows) {
 
        // second while loop is used for printing spaces
        while (sp < (rows - i - 1)) {
            printf("  ");
            sp++;
        }
 
        // assigning sp value as 0 because we need to run sp
        // from starting
 
        sp = 0;
       
        // this loop will print the pattern
        while (j <= i) {
            printf("* ");
            j++;
        }
 
        j = 0;
        i++;
        printf("\n");
    }
    return 0;
}


Output

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

Time complexity: O(n2) for given input n rows

Auxiliary space: O(1) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads