Java Online Compiler
Example: Diamond Pattern in Java using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// 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"); } } }
5
Output
Clear
ADVERTISEMENTS