VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-for-printing-inverted-pyramid/

⇱ C Program For Printing Inverted Pyramid - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program For Printing Inverted Pyramid

Last Updated : 10 Dec, 2024

An Inverted Pyramids are inverted triangular patterns where the base is at the top, and the rows decrease in size as they move downwards. In this article, we will learn how to print different types of inverted pyramid patterns using C program.

There can be 3 types of inverted pyramid patterns:


Inverted Right Half Pyramid

The Inverted Right Half Pyramid Pattern is a triangular pattern where the largest row is at the top and the number of characters in a row decreases as we move downwards. Each row is aligned to the left, resembling inverted right-angle triangle with its hypotenuse in right direction.


Output

* * * * * | 1 2 3 4 5 | A B C D E 
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Explanation:

  • Outer loop iterates over the rows starting from 0 to n - 1.
  • Inner loop controls the number of stars (*) (or number/character) printed on each row. The number of stars printed decreases as i increases. Initially, when i = 0, it prints n stars, and when i = 1, it prints n - 1 stars, and so on.

Inverted Left Half Pyramid

The Inverted Left Half Pyramid Pattern is a triangular pattern similar to the right half pyramid, but the characters are aligned to the right instead of left.


Output

* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A

Explanation:

  • The outer loop iterates over the rows, starting from i = 0 to i = n - 1.
  • First inner loop controls the number of white spaces printed on each row which increases as i increases.
  • Second inner loop controls the number of stars (*) (or number/character) printed on each row which decreases as i increases.

Inverted Full Pyramid

The Inverted Full Pyramid Pattern is a variation of the pyramid pattern where the largest row is at the top, and the number of characters decreases as we move downward. It is basically the inverted equilateral triangle.


Output

* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A

Explanation:

  • The outer loop iterates over the rows, from i = 0 to i = n - 1.
  • First inner loop prints leading white spaces on each row.The number of spaces increases as i increases, with 2 * i spaces on row i.
  • Second inner loop prints stars (*) (or number/character) on each row. The number of stars decreases as i increases. Initially, it prints 2 * (n - i) - 1 stars, reducing with each row.
Comment
Article Tags: