C Program to Print Hollow Diamond Pattern

Introduction

A hollow diamond pattern consists of stars (*) forming the boundary of the diamond shape, with the inner part being hollow (filled with spaces). This exercise helps programmers practice using loops and conditional statements to control output formatting.

Problem Statement

Create a C program that:

  • Accepts the number of rows for the upper half of the diamond.
  • Prints a hollow diamond pattern using stars (*).

Example:

  • Input: rows = 5
  • Output:
        *
       * *
      *   *
     *     *
    *       *
     *     *
      *   *
       * *
        *
    

Solution Steps

  1. Input the Number of Rows: The number of rows determines the height of the diamond.
  2. Use Nested Loops: The outer loop handles the rows, and the inner loops handle printing stars and spaces.
  3. Display the Hollow Diamond: Print stars at the boundary and spaces inside to create the hollow effect for both the upper and lower parts of the diamond.

C Program

#include <stdio.h>

int main() {
    int i, j, rows;

    // Step 1: Accept the number of rows for the upper half of the diamond
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Step 2: Print the upper half of the diamond
    for (i = 1; i <= rows; i++) {
        // Step 3: Print spaces for alignment
        for (j = i; j < rows; j++) {
            printf(" ");
        }

        // Step 4: Print stars and spaces for the hollow part
        for (j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");  // Print star at the boundary
            } else {
                printf(" ");  // Print space inside the diamond
            }
        }

        // Move to the next line
        printf("\n");
    }

    // Step 5: Print the lower half of the diamond
    for (i = rows - 1; i >= 1; i--) {
        // Step 6: Print spaces for alignment
        for (j = rows; j > i; j--) {
            printf(" ");
        }

        // Step 7: Print stars and spaces for the hollow part
        for (j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");  // Print star at the boundary
            } else {
                printf(" ");  // Print space inside the diamond
            }
        }

        // Move to the next line
        printf("\n");
    }

    return 0;
}

Explanation

Step 1: Input Number of Rows

  • The program starts by taking the input for the number of rows, which determines the height of both the upper and lower parts of the diamond.

Step 2-4: Print the Upper Part of the Diamond

  • The first loop handles the upper part of the diamond, including the middle row.
  • Spaces are printed to align the stars (*) properly. Stars are printed at the boundary, while spaces are printed inside to create the hollow effect.

Step 5-7: Print the Lower Part of the Diamond

  • The second loop handles the lower inverted part of the diamond.
  • Spaces are printed to align the stars, and stars are printed at the boundary with spaces inside.

Output Example

For rows = 5, the output will be:

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

For rows = 6, the output will be:

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

Conclusion

This C program prints a hollow diamond using stars (*). The program uses loops to print stars at the boundary and spaces inside to create the hollow effect. This exercise is useful for understanding how to manipulate loops and conditional statements to format output in C.

Comments