Hollow Diamond in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    int n=4;
    for(int i=1;i<=n;i++){
        for(int s=1;s<=n-i;s++) printf(" ");
        for(int j=1;j<=2*i-1;j++) printf(j==1||j==2*i-1?"*":" ");
        printf("\n");
    }
    for(int i=n-1;i>=1;i--){
        for(int s=1;s<=n-i;s++) printf(" ");
        for(int j=1;j<=2*i-1;j++) printf(j==1||j==2*i-1?"*":" ");
        printf("\n");
    }
}

C Output

Input:  
4  

Output:  
   *
  * *
 *   *
*     *
 *   *
  * *
   *


C++ Program

#include <iostream>
using namespace std;
int main(){
    int n=5;
    for(int i=1;i<=n;i++){
        for(int s=1;s<=n-i;s++) cout<<" ";
        for(int j=1;j<=2*i-1;j++) cout<<(j==1||j==2*i-1?"*":" ");
        cout<<"\n";
    }
    for(int i=n-1;i>=1;i--){
        for(int s=1;s<=n-i;s++) cout<<" ";
        for(int j=1;j<=2*i-1;j++) cout<<(j==1||j==2*i-1?"*":" ");
        cout<<"\n";
    }
}

C++ Output

Input:  
5  

Output:  
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *


JAVA Program

class Main {
    public static void main(String[] args) {
        int n=3;
        for(int i=1;i<=n;i++){
            for(int s=1;s<=n-i;s++) System.out.print(" ");
            for(int j=1;j<=2*i-1;j++) System.out.print(j==1||j==2*i-1?"*":" ");
            System.out.println();
        }
        for(int i=n-1;i>=1;i--){
            for(int s=1;s<=n-i;s++) System.out.print(" ");
            for(int j=1;j<=2*i-1;j++) System.out.print(j==1||j==2*i-1?"*":" ");
            System.out.println();
        }
    }
}

JAVA Output

Input:  
3  

Output:  
  *
 * *
*   *
 * *
  *


Python Program

n=6
for i in range(1,n+1):
    print(" "*(n-i) + "".join("*" if j==0 or j==2*i-2 else " " for j in range(2*i-1)))
for i in range(n-1,0,-1):
    print(" "*(n-i) + "".join("*" if j==0 or j==2*i-2 else " " for j in range(2*i-1)))

Python Output

Input:  
6 

Output:  
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *


Deep-Dive, Detailed Explanation
The Pattern
A hollow diamond consists of two reflected hollow triangles: an upward and a downward one. Every line has two components — spaces at the beginning to position the stars, and stars on the edges with spaces in between to create the hollow effect. The upper half gets wider, and the lower half gets narrower in a balanced manner.

Example
For n = 4, the logic for placing stars:

Row 1: 3 spaces + *

Row 2: 2 spaces + * space *

Row 3: 1 space + * 3 spaces *

Row 4: 0 spaces + * 5 spaces *
Then reversed for the bottom half.

Real-Life Analogy
Imagine a kite shape constructed from sticks. The stars are the sticks, and the paper space within is blank. This pattern copies the line of a diamond, which can also be said to copy the shape of a lampshade frame or the shape of an ornamental lantern.

Why It Matters
This exercise enhances your conditional printing control within nested loops. A hollow diamond is different from a solid one in that it takes additional decision making: you print only stars at the border but not in the middle. It's excellent practice for learning if-else within loops.

Learning Insights
Space Management: Spaces establish horizontal position, not stars.

Conditional Loops: Apply if to determine where stars appear.

Symmetry Logic: Knowing top and bottom half mirroring is central to most patterns.

In Interviews
Interviewers prefer hollow patterns because they introduce complexity. Most newcomers can draw a good diamond, but a hollow diamond requires knowledge of pattern structure and loop conditions. It's also an intermediate step to drawing shapes in graphics programming or ASCII art generators.

SEO-Friendly Closing
The empty diamond star pattern is a pretty-looking C, C++, Java, and Python programming problem that instructs alignment, conditional printing, and symmetry with nested loops. Learning this question enhances your console output formatting, so it's a useful one to master for competitive programming, coding interviews, and text-based user interface design.