Java Print Diamond
# Java Example β Print Diamond
[ Java Example](#)
Print a diamond pattern with a specified number of lines.
## Example
public class Diamond{public static void main(String[]args){print(8); // Print a diamond with 8 lines}public static void print(int size){if(size % 2 == 0){size++; // Calculate the size of the diamond}for(int i = 0; ii + 1; j--){System.out.print(""); // Print blank space for the top-left position}for(int j = 0; j<2 * i + 1; j++){System.out.print("*"); // Print the upper half edge of the diamond}System.out.println(); // New line}for(int i = size / 2 + 1; i<size; i++){for(int j = 0; j<i - size / 2; j++){System.out.print(""); // Print blank space for the bottom-left of the diamond}for(int j = 0; j<2 * size - 1 - 2 * i; j++){System.out.print("*"); // Print the lower half edge of the diamond}System.out.println(); // New line}}}
Output:
* *** ***** ******* ********* ******* ***** *** Java Example](#)
YouTip