Diamond Pattern Using Stars: C, Python and Java
Four diamond star patterns (solid, hollow, half, palindromic) with loop traces, space formulas, and C, Python, and Java code for n=5.
A diamond star pattern for input n prints 2n-1 rows: an upper half where each row grows by two stars, and a lower half that mirrors it back down.
The four variants covered here (solid, hollow, solid half, and palindromic half) all share that two-half structure. The only thing that changes between them is what gets printed in each row’s interior.
How Every Diamond Pattern Is Structured
Every diamond pattern follows the same outer skeleton. Two loops run in sequence: one for the upper half (rows 1 to n) and one for the lower half (rows n-1 down to 1). The upper half expands; the lower half contracts.
For a diamond with n rows of expansion, the total row count is n + (n-1) = 2n-1. The widest row is at position n and is printed exactly once. That is the correct count. A common error in older references is to state “2n rows”, but the center row belongs only to the upper half and is not repeated.
Space and star counts for the upper half
Each row i in the upper half (i running from 1 to n) follows a fixed formula:
- Leading spaces:
n - i - Star count:
2*i - 1
At row 1, that means n-1 spaces and a single star: the tip of the diamond. At row n, that means 0 spaces and 2n-1 stars: the full width. Every row in between adds one space less and two stars more.
The lower half mirrors the upper half exactly. Row i in the lower half (i running from n-1 down to 1) uses the same formula: n-i leading spaces, 2i-1 stars.
Loop trace for the solid diamond, n=5
| Row (i) | Leading spaces (n-i) | Stars (2i-1) | Output |
|---|---|---|---|
| 1 | 4 | 1 | * |
| 2 | 3 | 3 | *** |
| 3 | 2 | 5 | ***** |
| 4 | 1 | 7 | ******* |
| 5 | 0 | 9 | ********* |
| 4 | 1 | 7 | ******* |
| 3 | 2 | 5 | ***** |
| 2 | 3 | 3 | *** |
| 1 | 4 | 1 | * |
Nine rows total for n=5. Row 5 is the widest (9 stars). Row 1 appears once at the top and once at the bottom.
Solid Diamond Pattern
The solid diamond fills every row completely with stars. The program needs two outer loops (upper and lower half) and two inner loops per row (spaces, then stars).
C
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter n: ");
scanf("%d", &n);
/* Upper half */
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}
/* Lower half */
for (i = n - 1; i >= 1; i--) {
for (j = 1; j <= n - i; j++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}
return 0;
}
Python
n = int(input("Enter n: "))
# Upper half
for i in range(1, n + 1):
print(' ' * (n - i) + '*' * (2 * i - 1))
# Lower half
for i in range(n - 1, 0, -1):
print(' ' * (n - i) + '*' * (2 * i - 1))
Python’s string multiplication (' ' * k and '*' * k) replaces both inner loops. The lower half uses range(n - 1, 0, -1), which produces i = n-1, n-2, …, 1, excluding n (already printed) and stopping before 0.
Java
import java.util.Scanner;
public class SolidDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// Upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
// Lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
}
Output for n=5 across all three versions is identical to the trace table in the previous section.
Hollow Diamond Pattern
The hollow diamond keeps the same outer shape but replaces the filled star rows with border-only rows. Only the border stars are printed; the interior is left as spaces.
Three row types need separate handling:
- Row 1 (tip): n-1 leading spaces, then a single
*. No right border; there is nothing to the right of the tip. - Rows 2 to n-1 (sides): n-i leading spaces, then
*, then 2i-3 inner spaces, then*. - Row n (widest): 0 leading spaces, then
*, then 2n-3 inner spaces, then*.
The inner-space count for the sides follows from the total row width. At row i, the full width is 2i-1 characters. Subtracting the two border stars leaves 2i-3 spaces in the interior.
Loop trace for the hollow diamond, n=5
| Row (i) | Leading spaces | Inner spaces (2i-3) | Output |
|---|---|---|---|
| 1 | 4 | n/a (tip) | * |
| 2 | 3 | 1 | * * |
| 3 | 2 | 3 | * * |
| 4 | 1 | 5 | * * |
| 5 | 0 | 7 | * * |
| 4 | 1 | 5 | * * |
| 3 | 2 | 3 | * * |
| 2 | 3 | 1 | * * |
| 1 | 4 | n/a (tip) | * |
Inner-space check for i=2: 2*2-3 = 1. Row shows * * = 3 leading spaces + star + 1 space + star. ✓
C
#include <stdio.h>
int main() {
int n, i, j;
scanf("%d", &n);
/* Upper half */
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) printf(" ");
if (i == 1) {
printf("*\n");
} else {
printf("*");
for (j = 1; j <= 2 * i - 3; j++) printf(" ");
printf("*\n");
}
}
/* Lower half */
for (i = n - 1; i >= 1; i--) {
for (j = 1; j <= n - i; j++) printf(" ");
if (i == 1) {
printf("*\n");
} else {
printf("*");
for (j = 1; j <= 2 * i - 3; j++) printf(" ");
printf("*\n");
}
}
return 0;
}
Python
n = int(input("Enter n: "))
def hollow_row(i, n):
spaces = ' ' * (n - i)
if i == 1:
return spaces + '*'
return spaces + '*' + ' ' * (2 * i - 3) + '*'
# Upper half
for i in range(1, n + 1):
print(hollow_row(i, n))
# Lower half
for i in range(n - 1, 0, -1):
print(hollow_row(i, n))
The helper function hollow_row keeps the conditional logic in one place, making both loops identical. The Python range() function in the lower half uses range(n - 1, 0, -1), same as the solid diamond, so only the row-content logic changes between the two programs.
Java
import java.util.Scanner;
public class HollowDiamond {
static void printRow(int i, int n) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
if (i == 1) {
System.out.println("*");
} else {
System.out.print("*");
for (int j = 1; j <= 2 * i - 3; j++) System.out.print(" ");
System.out.println("*");
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
for (int i = 1; i <= n; i++) printRow(i, n);
for (int i = n - 1; i >= 1; i--) printRow(i, n);
}
}
Half Diamond Variants
The two half diamond variants drop the leading-space centering. Each row starts at the left margin.
Solid Half Diamond
The solid half diamond is the simplest of the four patterns. Row i prints i stars with no leading spaces. For n=5:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Stars are space-separated in this variant, matching the standard placement-test format. The loop structure is the same two-half skeleton but without the leading-space inner loop.
n = int(input("Enter n: "))
# Upper half
for i in range(1, n + 1):
print(' '.join(['*'] * i))
# Lower half
for i in range(n - 1, 0, -1):
print(' '.join(['*'] * i))
'*' * i would print stars without spaces. Using ' '.join(['*'] * i) inserts the separator. Both forms appear in practice; check the expected output format before submitting.
Palindromic Half Diamond
The palindromic half diamond combines a palindrome number sequence with star borders. Each row i (0-indexed) prints *, then the numbers 1 to i palindromically, then *. Row 0 is just a bare *.
For n=3, the output is 2n+1=7 rows:
*
* 1 *
* 1 2 1 *
* 1 2 3 2 1 *
* 1 2 1 *
* 1 *
*
Row structure for row i (i from 0 to n):
- i=0: print
* - i=1:
* 1 * - i=2:
* 1 2 1 * - i=n:
* 1 2 ... n ... 2 1 *
The inner number sequence is ascending from 1 to i, then descending from i-1 back to 1. This is the same palindrome-row logic as the palindrome pyramid pattern, wrapped with star borders instead of centering spaces.
n = int(input("Enter n: "))
def palindromic_row(i):
if i == 0:
return '*'
nums = list(range(1, i + 1)) + list(range(i - 1, 0, -1))
return '* ' + ' '.join(str(x) for x in nums) + ' *'
# Upper half (i from 0 to n)
for i in range(0, n + 1):
print(palindromic_row(i))
# Lower half (i from n-1 down to 0)
for i in range(n - 1, -1, -1):
print(palindromic_row(i))
Trace for i=3: nums = [1, 2, 3, 2, 1], output = * 1 2 3 2 1 *. ✓
The lower half uses range(n - 1, -1, -1), which includes i=0 (the bottom tip). The Python range() documentation returns an empty sequence when start already satisfies the stop condition for the given step direction. For row 0, range(0, 0, -1) meets that condition immediately. Row 0 prints only * as expected.
Diamond Patterns in Campus Placement Tests
Pattern printing problems appear regularly in the programming sections of campus recruitment tests for Indian engineering students, particularly at Tier-2 and Tier-3 colleges where test batteries include one or two pattern questions alongside standard data-structures problems.
The diamond pattern is a common harder variant precisely because it demands two coordinated loops per row (spaces and stars) plus a mirrored second half. A student who has only drilled right-aligned triangles will stall here. The hollow diamond adds another layer: a conditional branch that changes the row output depending on whether the row is the tip, a side, or the base. The palindromic half diamond requires three separate loop constructs per row and correct edge-case handling for row 0.
The most frequent failure mode is off-by-one in the inner loops. The star-count formula is 2i-1 (not 2i or 2i+1). The inner-space formula for the hollow variant is 2i-3 (not 2i-2 or 2i-1). Getting both correct without a trace is the standard skill gap.
For students preparing for IT jobs for freshers, pattern programs fall in the programming section of mass-hire company assessments. Diamond variants are achievable in under twelve minutes once the loop structure is clear, but that clarity requires having traced the loops explicitly, not just having read the output format once.
The same systematic approach applies to other output-formatting problems that appear in campus tests. The Tsunami report problem is a different kind of formatting challenge: no nested loops, but precise output alignment. Both problem types test the same underlying skill: translating a required output shape into a sequence of print statements with the correct counts and delimiters.
GeeksforGeeks covers diamond shape variants with additional language examples for those who want alternate implementation styles.
The nested-loop discipline that builds a diamond, tracking a row index, deriving space and star counts from a formula, mirroring the sequence in reverse, is the same structural thinking behind building iterative sequences in LLM applications. TinkerLLM is where to put that discipline to work building actual LLM tools rather than printing patterns; the entry point is ₹299.
Primary sources
Frequently asked questions
How many rows does a diamond star pattern have for input n?
A standard diamond pattern for input n has 2n-1 rows: n rows in the upper half (expanding) and n-1 rows in the lower half (contracting). The widest row at position n is the center and is printed once.
What is the leading-space formula for the solid diamond pattern?
At row i in the upper half, leading spaces equal n minus i (n-i). Row 1 gets n-1 spaces; row n gets 0 spaces. The lower half mirrors the upper half using the same formula.
How is the hollow diamond different from the solid diamond?
The hollow diamond prints only two border stars per row instead of a filled block of stars. Rows 2 to n-1 print one star, then 2i-3 inner spaces, then one star. Only the first row (tip) and the widest row differ slightly.
What is the difference between a solid and a half diamond pattern?
The solid diamond is centered with leading spaces. The half diamond drops the leading-space loop entirely, so each row starts at the left margin. The star count per row is the same in both.
Does the palindromic half diamond appear in campus placement coding tests?
Yes. It is a harder variant that combines ascending and descending number loops with star borders. It tests whether a candidate can manage three separate loop constructs per row and handle the edge case where row 0 prints only a bare star.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)