Open In App

C Program To Print Hollow Diamond Pattern

Last Updated : 10 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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
Star Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // First outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        
        // Assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) comp = 2 * (n - i) - 1;
        else comp = 2 * (i - n + 1) + 1;

        // First inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++)
            printf(" ");

        // Second inner loop to print star * and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
    
    return 0;
}
Number Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // First outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        
        // Assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) comp = 2 * (n - i) - 1;
        else comp = 2 * (i - n + 1) + 1;

        // First inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++)
            printf(" ");

        // Second inner loop to print star * and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1)
                printf("%d ", k + 1);
            else
                printf("  ");
        }
        printf("\n");
    }
    
    return 0;
}
Alphabet Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // First outer loop to iterator through each row
    for (int i = 0; i < 2 * n - 1; i++) {
        
        // Assigning values to the comparator according to
        // the row number
        int comp;
        if (i < n) comp = 2 * (n - i) - 1;
        else comp = 2 * (i - n + 1) + 1;

        // First inner loop to print leading whitespaces
        for (int j = 0; j < comp; j++)
            printf(" ");

        // Second inner loop to print star * and inner
        // whitespaces
        for (int k = 0; k < 2 * n - comp; k++) {
            if (k == 0 || k == 2 * n - comp - 1)
                printf("%c ", k + 'A');
            else
                printf("  ");
        }
        printf("\n");
    }
    
    return 0;
}


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 *.


Next Article

Similar Reads