C Program
#include <stdio.h> int main(){ int n=4; for(int i=1;i<=n;i++){ for(int j=i;j<n;j++) printf(" "); for(int j=1;j<=2*i-1;j++) printf("*"); printf("\n"); } for(int i=n-1;i>=1;i--){ for(int j=i;j<n;j++) printf(" "); for(int j=1;j<=2*i-1;j++) printf("*"); printf("\n"); } }
C Output
Input: n = 4 Output: * *** ***** ******* ***** *** *
C++ Program
#include <iostream> using namespace std; int main(){ int n=3; for(int i=1;i<=n;i++){ cout<<string(n-i,' ')<<string(2*i-1,'*')<<"\n"; } for(int i=n-1;i>=1;i--){ cout<<string(n-i,' ')<<string(2*i-1,'*')<<"\n"; } }
C++ Output
Input: n = 3 Output: * *** ***** *** *
JAVA Program
public class Main{ public static void main(String[] args){ int n=5; for(int i=1;i<=n;i++){ System.out.println(" ".repeat(n-i)+"*".repeat(2*i-1)); } for(int i=n-1;i>=1;i--){ System.out.println(" ".repeat(n-i)+"*".repeat(2*i-1)); } } }
JAVA Output
Input: n = 5 Output: * *** ***** ******* ********* ******* ***** *** *
Python Program
n=2 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
Input: n = 2 Output: * *** *
Deep-Dive Explanation
Recognizing the Diamond Pattern
The diamond pattern is made up of two pyramids: one upright pyramid at the top and one inverted pyramid at the bottom. The number of stars rises till middle and decreases symmetrically.
Step-by-Step Logic
We divide it into two parts. First, the top half:
Loop from i = 1 to n for rows.
Print (n-i) spaces for alignment.
Print (2*i - 1) stars to create the width.
Go to the next line.
And then the bottom half:
Loop from i = n-1 to 1.
Use the same space and star printing logic.
Example for n = 4
Upper half:
Row 1 → 3 spaces + 1 star
Row 2 → 2 spaces + 3 stars
Row 3 → 1 space + 5 stars
Row 4 → 0 spaces + 7 stars
Lower half: Reverse the upper half without the middle row.
Why This Pattern Matters
Mastering this pattern reinforces knowledge of nested loops, repeating strings, and symmetry in reasoning. It's widespread in coding interview questions to assess nested loop expertise.
Real-Life Analogy
Imagine a diamond kite — it's constructed with immaculate symmetry starting from the middle line, identical to this pattern.
Practical Uses
Console application text-based artwork
Terminal UI loading animations
Center printing logic within formatted outputs
Interview Insights
Hollow diamonds, number diamonds, or alphabet diamonds are some of the variations. If you can code one, it is easy to switch to the rest by modifying the print logic.
For SEO purposes:
The diamond pattern C program, C++ diamond pattern program, Java diamond pattern program, and Python diamond pattern program are the most popular initial programming puzzles. They learn us the usage of nested loops, management of space, and center positioning logic. If you are preparing for coding interviews, wanting to practice console-based pattern programs, or studying fundamental loop concepts, knowing the diamond pattern will make your problem-solving ability stronger.
Social Plugin