VOOZH about

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

⇱ C Program To Print Hollow Diamond Pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program To Print Hollow Diamond Pattern

Last Updated : 10 Dec, 2024

The Hollow Diamond Pattern is a variation of the diamond pattern where only the edges of the diamond are filled with characters and the inside remains empty. This creates a hollow effect in the shape of a diamond. In this article, we will learn how to print the Hollow Diamond Pattern using C program.

👁 Hollow-Diamond-Pattern__
Hollow Diamond Pattern


Output

 * | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * | 1 9 | A I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A

Explanation:

  • Outer loop iterates through each row from 0 to 2 * n - 2 (as total 2 * n - 1 rows).
  • comp determines the number of leading spaces based on the current row number.
    • For the first half (i < n): comp = 2 * (n - i) - 1.
    • For the second half (i >= n): comp = 2 * (i - n + 1) + 1.
  • First inner loop prints leading white spaces in each row.
  • Second inner loop iterates through the columns in the row to print the diamond's outline.
    • For the first (k == 0) or last star position in the row (k == 2 * n - comp - 1), it prints *.
    • For positions in between, it prints spaces (" "), leaving the diamond hollow.
  • After completing a row, printf("\n") moves to the next line.

The above explanation is for * pattern only. You can use any character to place of star *.

Comment
Article Tags: