Javatpoint Logo
Javatpoint Logo

Hollow Diamond Pattern in C

Introduction:

Programmers write a lot of practice programs in the form of patterns. One of them is the diamond pattern programs. Here, we'll use C to create a hollow diamond design.

Star pattern in C with hollow diamonds

Let's create a C program for star pattern with hollow diamonds:

Output:

Enter number of rows: 7

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

Numbering Pattern in a Hollow Diamond:

Instead of stars, this hollow diamond design has numbers. Therefore, only a small portion of the code will change, while the rest will remain unchanged.

The pattern counts up from 1 to 5 starting with 1. Only 1 appears in the first and last rows, whereas exactly 2 identical numbers appear in all other rows. The size of the design increases from the first to the fifth row while decreasing from the sixth to the ninth row.

Output:

Input: 5

     1
    2 2
   3   3
  4     4
 5       5
  4     4
   3   3
    2 2
     1

In this design, the first and last rows each have one star, and the remaining rows all have precisely two stars. Simply replace the * with another character to display any character in lieu of the stars. In this instance, though, each character must be unique.

We'll employ the following 2 methods to print the hollow diamond pattern in C:

  1. Using for loop
  2. Using while loop

First approach: Using for Loop

Output:

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

Another approach: Using While Loop

Now we can print the hollow diamond using while loop:

Output:

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






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