In this tutorial, we will write a java program to print diamond pattern of stars. Before that, you should have knowledge on the following topic in Java.
Java Program to Print Diamond Pattern
Source Code: the program asks the user for the input of a number of rows for the diamond star pattern in java. Then using for loop, the diamond shape pattern is displayed on the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | // Print Diamond star Pattern in java import java.util.Scanner; public class Main { public static void main(String args[]) { int n, j, i, space = 1; Scanner scan = new Scanner(System.in); System.out.print("Enter the no. of Rows: "); n = scan.nextInt(); space = n - 1; for (i = 1; i <= n; i++) { for (j = 1; j <= space; j++) { System.out.print(" "); } space--; for (j = 1; j <= (2 *i - 1); j++) { System.out.print("*"); } System.out.println(); } space = 1; for (i = 1; i <= (n - 1); i++) { for (j = 1; j <= space; j++) { System.out.print(" "); } space++; for (j = 1; j <= (2 *(n - i) - 1); j++) { System.out.print("*"); } System.out.println(); } } } |
Output: