Diamond Pattern in Java using For loop
ADVERTISEMENTS
Diamond pattern in java using for loop. In this article, you will learn how to print the diamond pattern in java using for loop.
Source Code
// Diamond Pattern in Java using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x, c, k, s = 1;
System.out.print("-----Enter the number of rows-----\n");
x = in.nextInt();
s = x - 1;
for (k = 1; k <= x; k++) {
System.out.print("\t");
for (c = 1; c <= s; c++)
System.out.print(" ");
s--;
for (c = 1; c <= 2 * k - 1; c++)
System.out.print("*");
System.out.print("\n");
}
s = 1;
for (k = 1; k <= x - 1; k++) {
System.out.print("\t");
for (c = 1; c <= s; c++)
System.out.print(" ");
s++;
for (c = 1 ; c <= 2 * (x - k) - 1; c++)
System.out.print("*");
System.out.print("\n");
}
}
}
Output
-----Enter the number of rows-----
5
*
***
*****
*******
*********
*******
*****
***
*
5
*
***
*****
*******
*********
*******
*****
***
*