Javatpoint Logo
Javatpoint Logo

C Program For Printing Inverted Pyramid

In this section, we'll look at how to print an inverted pyramid with a C language. Here are several examples:

Method 1: The pattern is split into three sections:

  1. The vacant spaces will be printed using a for loop.
  2. To print the triangle from the left side, a for loop will be needed.
  3. A for loop is going to be utilized to print the remaining triangle, resulting in the perfect design.

Algorithm:

  1. Set variables i, j, and space to represent rows, columns, and blank spaces, accordingly.
  2. The number of rows in this example is 7, but the user may select any number.
  3. Create a for loop that will serve as the main loop in printing the pattern and will control the other loops contained within it.
  4. Begin a for loop to print the blank spaces within the main loop.
  5. To begin printing the star design, first print the half pyramid as illustrated below. To do so, we'll start a for loop with the condition 2 * i - 1, so the number of stars is odd.
  6. Following that, we'll apply the other for loop to print the remainder of the pattern.
C Program For Printing Inverted Pyramid

The C programme to generate an inverted pyramid design is shown below:

C Programming Language:

Output

* * * * * * * * * * * * * 
 * * * * * * * * * * * 
  * * * * * * * * * 
   * * * * * * * 
    * * * * * 
     * * * 
      *
  • Time Complexity will be O(n2).
  • Auxiliary Space will be O(1).

Method 2: Require two for loops:

  1. The vacant spaces will be printed using a for loop.
  2. To print the stars, a for loop will be utilised.

Algorithm:

  1. Set variables I j, and rows to their initial values.
  2. The row count here is seven, but the user can choose any number.
  3. Create a for loop that will serve as the main loop in displaying the pattern and will drive the other loops contained within it.
  4. Create a for loop that will run from 1 to I and will print the blanks inside the main loop.
  5. To print the stars, run a for loop from 1 through rows * 2 - I * 2 - 1).
  6. With the help of the new line character, go to the following line.

The C code to produce an inverted pyramid design is shown below:

C Programming Language:

Output

*************
 ***********
  *********
   *******
    *****
     ***
      *
  • Time Complexity will be O(n2).
  • Auxiliary Space will be O(1).






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA