VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-print-hourglass-pattern/

⇱ C Program to Print Hourglass Pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Print Hourglass Pattern

Last Updated : 13 Dec, 2024

The Hourglass Pattern is a symmetrical pattern similar to an hourglass shape. It can be visualized as an inverted full pyramid placed on a regular full pyramid. In this article, we will learn how to print the Hourglass Pattern using a C program.

👁 Hourglass-Pattern

Program to Print Hourglass Pattern in C


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
* * * | 1 2 3 | A B C
* * * * * | 1 2 3 4 5 | A B C D E
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

Explanation:

  • The outer loop runs for 2 * n - 1 iterations for printing each row of the pattern.
    • For rows i < n, it processes the top half (inverted pyramid).
    • For rows i >= n, it processes the bottom half (regular pyramid).
  • The l variable determines the relative position of the current row from the top or bottom center:
    • Top half: l = i.
    • Bottom half: l = 2 * n - 2 - i.
  • The first inner loop prints the number of leading spaces.
  • The second inner loop prints the stars (or numbers/alphabet).
Comment
Article Tags: