- Java program to print hollow diamond star pattern using for loop.
Sample Output,
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Java program to print hollow diamond star pattern
package com.tcc.java.programs;
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] arg) {
int rows, i, space, star = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Rows of Hollow Diamond Pattern");
rows = scanner.nextInt();
/* PRINTING UPPER TRIANGLE */
// printing one row in every iteration
for (i = 1; i <= rows; i++) {
// Printing spaces
for (space = 1; space <= rows - i; space++) {
System.out.print(" ");
}
// Printing stars
while (star != (2 * i - 1)) {
// Print only the first and last star of row
if (star == 0 || star == (2 * i - 2))
System.out.print("*");
else
System.out.print(" ");
star++;
}
star = 0;
// move to next row
System.out.print("\n");
}
rows--;
/* PRINTING LOWER TRIANGLE */
for (i = rows; i >= 1; i--) {
/* Printing spaces */
for (space = 0; space <= rows - i; space++) {
System.out.print(" ");
}
/* Printing stars */
star = 0;
while (star != (2 * i - 1)) {
// Print only the first and last star of row
if (star == 0 || star == (2 * i - 2))
System.out.print("*");
else
System.out.print(" ");
star++;
}
System.out.print("\n");
}
}
}
Output
Enter Number of Rows of Hollow Diamond Pattern
7
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*