Java Online Compiler
Example: Hollow Diamond Pattern in Java language using Function
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Hollow Diamond Pattern in Java language using Function import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x; // x - denotes the size of the hollow diamond System.out.println("-----Enter the size of the hollow diamond, it should be even-----"); x = in.nextInt(); // The size of the hollow diamond should be even number if (x % 2 == 1) x++; // This will print the hollow diamond pattern hollowDiamond(x); } private static void hollowDiamond(int size) { int i, j, diff; diff = size / 2; System.out.println("\n-----The hollow diamond pattern is-----\n"); // This will print the first half diamond for (i = 1; i <= diff; i++) { System.out.print("\t"); for (j = 1; j <= diff - i; j++) { System.out.print(" "); } if (i == 1) { System.out.print("*"); } else { System.out.print("*"); for (j = 1; j <= 2 *i - 3; j++) { System.out.print(" "); } System.out.print("*"); } System.out.print("\n"); } // This will print the last half diamond for (i = diff + 1; i < size; i++) { System.out.print("\t"); for (j = 1; j <= i - diff; j++) { System.out.print(" "); } if (i == size - 1) { System.out.print("*"); } else { System.out.print("*"); for (j = 1; j <= 2 *(size - i) - 3; j++) { System.out.print(" "); } System.out.print("*"); } System.out.print("\n"); } } }
14
Output
Clear
ADVERTISEMENTS