Programming is always a fun and creative task. The more creative you can think the better programmer you will become. However, programming isn’t always about solving problems sometimes there’s also a fun aspect to it which makes it way more satisfying. If you are a programmer then you might have already played with patterns, strings, etc multiple times. Well, it’s also like that you are already familiar with printing patterns. So in today’s article, we will create a program to Print Diamond Shape Star Pattern in Java. With the help of this program, you will be able to print a diamond using the java language. Practising these types of questions also helps to get an upper edge in Competitive Programming.
We will be showing you different ways of printing diamond shape pattern in Java so that you can use whatever method you like to impress your classmates, friends or even your crush if he/she likes programming. So open up your IDE and let’s get started real quick. Once you understand the logic practice this program on your own to make your brain work in a problem-solving way. This is a somewhat hard question you might get asked in an interview so make sure to practice it on your own after reading this article.
Also Read: How to Install Java JDK JRE on Windows 11
Java Program To Print Diamond Shape Star Pattern With
1) do-while Loop
// Java program to Print Diamond Star Pattern
// Importing input output classes
public class TechDecodeTutorials {
public static void main(String[] args)
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// Diamond starting with single star in first row,so
// Prints the first half diamond
// Prints space until ++n <= number - m + 1 is
// Print whitespace between
// Condition for inside do-while loop 1
while (++n <= number - m + 1);
// Prints star until ++n <= m * 2 - 1 is false
// Condition for iner do-while loop 2
while (++n <= m * 2 - 1);
// A new row requires a new line
// Condition for outer do-while loop 1
// Now we are done with printing the upper half
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
// Prints the second half diamond
// Prints space until ++n <= number - m + 1 is
// Print whitespace between
} while (++n <= number - m + 1);
// Prints star until ++n <= m * 2 - 1 is false
} while (++n <= m * 2 - 1);
// By now done with one row of lower diamond
// printing so a new line is required
// Condition for outer do-while loop 2
// Java program to Print Diamond Star Pattern
// Using do-while loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
// Diamond starting with single star in first row,so
// initialized
int m = 1;
// Columnar printing
int n;
// Outer loop 1
// Prints the first half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
}
// Condition for inside do-while loop 1
while (++n <= number - m + 1);
// Now
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Print star
System.out.print("*");
}
// Condition for iner do-while loop 2
while (++n <= m * 2 - 1);
// A new row requires a new line
System.out.println();
}
// Condition for outer do-while loop 1
while (++m <= number);
// Now we are done with printing the upper half
// diamond.
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
m = number - 1;
// Outer loop 2
// Prints the second half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
} while (++n <= number - m + 1);
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Prints star
System.out.print("*");
} while (++n <= m * 2 - 1);
// By now done with one row of lower diamond
// printing so a new line is required
System.out.println();
}
// Condition for outer do-while loop 2
while (--m > 0);
}
}
// Java program to Print Diamond Star Pattern
// Using do-while loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
// Diamond starting with single star in first row,so
// initialized
int m = 1;
// Columnar printing
int n;
// Outer loop 1
// Prints the first half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
}
// Condition for inside do-while loop 1
while (++n <= number - m + 1);
// Now
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Print star
System.out.print("*");
}
// Condition for iner do-while loop 2
while (++n <= m * 2 - 1);
// A new row requires a new line
System.out.println();
}
// Condition for outer do-while loop 1
while (++m <= number);
// Now we are done with printing the upper half
// diamond.
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
m = number - 1;
// Outer loop 2
// Prints the second half diamond
do {
n = 1;
// Inner loop 1
// Prints space until ++n <= number - m + 1 is
// false
do {
// Print whitespace between
System.out.print(" ");
} while (++n <= number - m + 1);
n = 1;
// Inner loop 2
// Prints star until ++n <= m * 2 - 1 is false
do {
// Prints star
System.out.print("*");
} while (++n <= m * 2 - 1);
// By now done with one row of lower diamond
// printing so a new line is required
System.out.println();
}
// Condition for outer do-while loop 2
while (--m > 0);
}
}
Output
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
2) while Loop
// Java program to print diamond star pattern
// Importing input output classes
public class TechDecodeTutorials {
public static void main(String[] args)
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// Diamond starting with single star in first row
// Prints the first half diamond
// Condition holding true till
// number of rows initialized
// Prints space until n++ <= number - m is false
while (n++ <= number - m) {
// Print whitespaces inbetween
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// By now we are done for above pyramid printing
// ending line after each row
// Incrementing as we want pyramid generation
// Now we are done with printing the upper half
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
// Prints the second half diamond
// Prints spaces until n++ <= number - m is
while (n++ <= number - m) {
// Print whitespace in between
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// Ending line after each row
// Decrementing as we want reverse pyramid
// Java program to print diamond star pattern
// Using while loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
// Diamond starting with single star in first row
int m = 1;
// Columnar printing
int n;
// Outer loop 1
// Prints the first half diamond
// Condition holding true till
// number of rows initialized
while (m <= number) {
n = 1;
// Inner loop 1
// Prints space until n++ <= number - m is false
while (n++ <= number - m) {
// Print whitespaces inbetween
System.out.print(" ");
}
n = 1;
// Inner loop 2
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// Print star
System.out.print("*");
}
// By now we are done for above pyramid printing
// ending line after each row
System.out.println();
// Incrementing as we want pyramid generation
m++;
}
// Now we are done with printing the upper half
// diamond.
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
m = number - 1;
// Outer loop 2
// Prints the second half diamond
while (m > 0) {
n = 1;
// Inner loop 1
// Prints spaces until n++ <= number - m is
// false
while (n++ <= number - m) {
// Print whitespace in between
System.out.print(" ");
}
n = 1;
// Inner loop 2
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// Print star
System.out.print("*");
}
// Ending line after each row
System.out.println();
// Decrementing as we want reverse pyramid
// generation
m--;
}
}
}
// Java program to print diamond star pattern
// Using while loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
// Diamond starting with single star in first row
int m = 1;
// Columnar printing
int n;
// Outer loop 1
// Prints the first half diamond
// Condition holding true till
// number of rows initialized
while (m <= number) {
n = 1;
// Inner loop 1
// Prints space until n++ <= number - m is false
while (n++ <= number - m) {
// Print whitespaces inbetween
System.out.print(" ");
}
n = 1;
// Inner loop 2
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// Print star
System.out.print("*");
}
// By now we are done for above pyramid printing
// ending line after each row
System.out.println();
// Incrementing as we want pyramid generation
m++;
}
// Now we are done with printing the upper half
// diamond.
// Note: Not to print the bottom row again in lower
// half diamond printing Hence variable t be
// initialized should one lesser than number
m = number - 1;
// Outer loop 2
// Prints the second half diamond
while (m > 0) {
n = 1;
// Inner loop 1
// Prints spaces until n++ <= number - m is
// false
while (n++ <= number - m) {
// Print whitespace in between
System.out.print(" ");
}
n = 1;
// Inner loop 2
// Prints star until n++ <= m * 2 - 1 is false
while (n++ <= m * 2 - 1) {
// Print star
System.out.print("*");
}
// Ending line after each row
System.out.println();
// Decrementing as we want reverse pyramid
// generation
m--;
}
}
}
Output
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
3) Using for Loop
// Java program to print diamond star pattern
// Importing input output classes
public class TechDecodeTutorials {
public static void main(String[] args)
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// prints the first half diamond
for (m = 1; m <= number; m++) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
// Inner loop 2 prints star
for (n = 1; n <= m * 2 - 1; n++) {
// Ending line after each row
// Prints the second half diamond
for (m = number - 1; m > 0; m--) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
// Inner loop 2 print star
for (n = 1; n <= m * 2 - 1; n++) {
// Ending line after each row
// Java program to print diamond star pattern
// Using for loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
int m, n;
// Outer loop 1
// prints the first half diamond
for (m = 1; m <= number; m++) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
System.out.print(" ");
}
// Inner loop 2 prints star
for (n = 1; n <= m * 2 - 1; n++) {
System.out.print("*");
}
// Ending line after each row
System.out.println();
}
// Outer loop 2
// Prints the second half diamond
for (m = number - 1; m > 0; m--) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
System.out.print(" ");
}
// Inner loop 2 print star
for (n = 1; n <= m * 2 - 1; n++) {
System.out.print("*");
}
// Ending line after each row
System.out.println();
}
}
}
// Java program to print diamond star pattern
// Using for loop
// Importing input output classes
import java.io.*;
// Main class
public class TechDecodeTutorials {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variables
// Variable initialized to the row where max star
// should be there as after that they decreases to
// give diamond pattern
int number = 7;
int m, n;
// Outer loop 1
// prints the first half diamond
for (m = 1; m <= number; m++) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
System.out.print(" ");
}
// Inner loop 2 prints star
for (n = 1; n <= m * 2 - 1; n++) {
System.out.print("*");
}
// Ending line after each row
System.out.println();
}
// Outer loop 2
// Prints the second half diamond
for (m = number - 1; m > 0; m--) {
// Inner loop 1 print whitespaces inbetween
for (n = 1; n <= number - m; n++) {
System.out.print(" ");
}
// Inner loop 2 print star
for (n = 1; n <= m * 2 - 1; n++) {
System.out.print("*");
}
// Ending line after each row
System.out.println();
}
}
}
Output
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*