Skip to content
Related Articles

Related Articles

C Program to Print 180 Degree Rotation of Inverted Half Pyramid

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Aug, 2022

C program to print 180-degree rotation of inverted half pyramid pattern using two approaches i.e. for loop and while loop.

Input: Number = 5

Output:

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

Approach 1: Using for Loop

The first for loop is used to identify the number of rows, the second for loop is used to handle the spaces and the third for loop is used to identify the number of columns. Here the values will be changed according to the first for loop, then print the required pattern.

C




// C program to print 180 degree rotation of inverted
// pyramid pattern
#include <stdio.h>
 
int main()
{
    int number = 5;
    int k = 2 * number - 2;
 
    // first for loop is used to identify number of rows
    for (int rows = number; rows > 0; rows--) {
 
        // second for loop is used to handle the spaces
        for (int columns = 0; columns < number - rows;
             columns++)
            printf("  ");
 
        // finding k after each loop
        k = k - 2;
 
        // third for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (int columns = 0; columns < rows; columns++) {
            // Printing required pattern
            printf("* ");
        }
 
        // printing next line after each row
        printf("\n");
    }
    return 0;
}

Output

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

Time Complexity: O(n2)
Auxiliary Space: O(1)

Approach 2: Using while Loop

The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements. 

C




// C program to print 180 degree rotation of inverted
// pyramid pattern
#include <stdio.h>
 
int main()
{
    int number = 5;
    int rows = number, columns = 0, k = 0;
    // first while loop is used to identify number of rows
    while (rows > 0) {
 
        // second while loop is used to handle the spaces
        while (k < (number - rows)) {
            printf("  ");
            k++;
        }
        // assign k value to 0 because we need to start from
        // the beginning
        k = 0;
       
        // third while loop is used to identify number of
        // columns and here the values will be changed
        // according to the first while loop
        while (columns < rows) {
            printf("* ");
            columns++;
        }
 
        columns = 0;
        rows--;
        printf("\n");
    }
    return 0;
}

Output

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

Time Complexity: O(n2)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!