Hollow Concentric Diamond of Stars
This program prints hollow concentric diamonds using stars.
Problem Statement
Write a Java program that prints concentric hollow diamonds with 7 rows.
Source Code
| 1 | public class HollowConcentricDiamondStars { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 7; |
| 4 | for(int i=1; i<=n; i++) { |
| 5 | for(int j=i; j<n; j++) System.out.print(" "); |
| 6 | for(int j=1; j<=2*i-1; j++) { |
| 7 | if(j==1 || j==2*i-1) System.out.print("*"); |
| 8 | else System.out.print(" "); |
| 9 | } |
| 10 | System.out.println(); |
| 11 | } |
| 12 | for(int i=n-1; i>=1; i--) { |
| 13 | for(int j=i; j<n; j++) System.out.print(" "); |
| 14 | for(int j=1; j<=2*i-1; j++) { |
| 15 | if(j==1 || j==2*i-1) System.out.print("*"); |
| 16 | else System.out.print(" "); |
| 17 | } |
| 18 | System.out.println(); |
| 19 | } |
| 20 | } |
| 21 | } |
Program Output
*
* *
* *
* *
* *
* *
*
Explanation
The first loop builds a top hollow pyramid and the second builds an inverted hollow pyramid, both printing stars only at row boundaries, together forming a hollow diamond outline.