Print Star Patterns (Diamond) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

void diamond(int n) {
    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("*");
        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("*");
        printf("\n");
    }
}

C Output

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


C++ Program

void diamond(int n) {
    for (int i = 1; i <= n; i++, cout << "\n") {
        for (int s = 0; s < n-i; s++) cout << " ";
        for (int j = 0; j < 2*i-1; j++) cout << "*";
    }
    for (int i = n-1; i >= 1; i--, cout << "\n") {
        for (int s = 0; s < n-i; s++) cout << " ";
        for (int j = 0; j < 2*i-1; j++) cout << "*";
    }
}

C++ Output

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


JAVA Program

void diamond(int n) {
    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("*");
        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("*");
        System.out.println();
    }
}

JAVA Output

 *
***
 *


Python Program

def diamond(n):
    for i in range(1, n+1):
        print(' '*(n-i) + '*'*(2*i - 1))
    for i in range(n-1, 0, -1):
        print(' '*(n-i) + '*'*(2*i - 1))

Python Output

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


In-Depth Explanation
Example
Suppose n = 4. We construct:

An upper pyramid with rows 1 to 4.

A lower inverted pyramid with rows 3 to 1.

Each row prints:

Spaces = n - row

Stars = 2 * row - 1

For row 1:
Spaces: 3, Stars: 1 → *
For row 2:
Spaces: 2, Stars: 3 → ***
This repeats symmetrically downwards and upwards again.

Real-Life Analogy
Think about a kite or a diamond mirror — the form is symmetrical from top to bottom. Or consider adorning a tree where the top levels expand till the middle and then taper once again as you descend.

Another such instance is wedding decorations or festival rangoli designs where diamonds are drawn with lights or paper stars — symmetry and visual balance only.

Why It Matters
Diamond patterns learn:

Two-section logic handling (top & bottom)

Mastery of nested loops

Symmetry management

This is where simple pattern logic evolves into something more complex — dealing with multiple sections of logic with mirror-like characteristics.

It also introduces centered alignment, odd number pattern, and nested structure repetition — all crucial to UI rendering, CLI tools, and visual animation logic.

What You Learn from This
Building two sections of output with mirrored loops

Calculating the number of spaces and stars correctly

Improving visual thinking in code

Knowledge of odd number sequences and pattern generation

This pattern also improves your logic and visualization skills, which play important roles in layout generation, grid-based layouts, or even terminal-based games.

Interview Relevance and Real Projects
Diamond patterns are used commonly in:
College-level interviews
Logic and reasoning rounds
Basic to intermediate coding tests
Interviewers can make slight modifications to:
Use numbers in place of stars
Print half the diamond
Support variable characters

Real-world uses are:

ASCII art

Dynamic layout creation

Terminal visual feedback systems

Symmetrical matrix creation

SEO-Optimized Explanation
The diamond star pattern is a popular programming exercise that is taught to learn about symmetry, nested loops, and printing dynamic shapes. In this exercise, you output a top pyramid and a bottom inverted pyramid using stars (*), forming a complete diamond shape. Applying the diamond pattern in C, C++, Java, and Python makes your control over spaces, repetition of characters, and visual arrangement more efficient using loops. The application isn't limited to interviews alone but also in creating terminal-based graphics, CLI dashboards, text animations, and creating symmetrical layouts for console applications. Exercise in the diamond pattern enables students to improve their logic and flow control while coding in real-time.