C Program to print hollow pyramid and diamond pattern

In this tutorial, we will learn how to create hollow pyramid and diamond patterns in C programming. Unlike solid patterns, hollow patterns print stars only at the borders while keeping the interior empty using spaces.

Syntax

/* Hollow Pyramid Logic */
for(i = 1; i <= n; i++) {
    // Print leading spaces
    // Print first star
    // Print middle spaces (for hollow effect)  
    // Print last star (except first line)
}

Method 1: Hollow Pyramid Pattern

For the hollow pyramid, the first line prints one star, and the last line prints n stars. For middle lines, we print exactly two stars at the start and end positions with blank spaces between them −

#include <stdio.h>

int main() {
    int n, i, j;
    printf("Enter number of lines: ");
    scanf("%d", &n);
    
    for(i = 1; i <= n; i++) {
        /* Print leading spaces */
        for(j = 1; j <= (n-i); j++){
            printf(" ");
        }
        
        if(i == 1 || i == n){ 
            /* For first and last line, print stars continuously */
            for(j = 1; j <= i; j++) {
                printf("* ");
            }
        } else {
            /* For middle lines: star at start and end positions */
            printf("*");
            for(j = 1; j <= 2*i-3; j++) {
                printf(" ");
            }
            printf("*");
        }
        printf("
"); } return 0; }

The output of the above code is −

Enter number of lines: 8
       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *
* * * * * * * * 

Method 2: Hollow Diamond Pattern

The diamond pattern consists of two parts: upper and lower halves. The upper half expands outward, while the lower half contracts inward −

#include <stdio.h>

int main() {
    int n, i, j, mid;
    printf("Enter number of lines: ");
    scanf("%d", &n);
    
    if(n % 2 == 1) {
        n++; /* Make even for symmetric diamond */
    }
    
    mid = n / 2;
    
    /* Upper half of diamond */
    for(i = 1; i <= mid; i++) {
        for(j = 1; j <= (mid-i); j++){
            printf(" ");
        }
        
        if(i == 1) {
            printf("*");
        } else {
            printf("*");
            for(j = 1; j <= 2*i-3; j++){
                printf(" ");
            }
            printf("*");
        }
        printf("
"); } /* Lower half of diamond */ for(i = mid+1; i < n; i++) { for(j = 1; j <= i-mid; j++) { printf(" "); } if(i == n-1) { printf("*"); } else { printf("*"); for(j = 1; j <= 2*(n-i)-3; j++) { printf(" "); } printf("*"); } printf("
"); } return 0; }

The output of the above code is −

Enter number of lines: 10
     *
    * *
   *   *
  *     *
 *       *
  *     *
   *   *
    * *
     *

Key Points

  • Hollow patterns use conditional logic to print stars only at borders
  • The formula 2*i-3 calculates the number of spaces between two stars in hollow pyramid
  • Diamond pattern combines expanding and contracting hollow triangles

Conclusion

Hollow pyramid and diamond patterns demonstrate advanced loop control and spatial logic in C. These patterns are excellent for understanding nested loops and conditional printing techniques.

Updated on: 2026-03-15T10:07:14+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements