Java Online Compiler
Example: Print Star Pattern without using loop in Java language
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Print Star Pattern without using loop in Java language import java.util.Scanner; public class Main { public static int i = 1, r = 1; public static void main(String[] args) { Scanner in = new Scanner(System.in); // size of the pattern int x; System.out.println("-----Enter the size of the pattern-----"); x = in .nextInt(); // This will print the pattern starPattern(x); } // It's the recursive function // to print the star pattern public static void starPattern(int n) { if ((int) Math.sqrt(Math.pow((i - (2 * n - 1) * (r - 1) - n), 2))<r) { System.out.print("*"); } else { System.out.print(" "); } if ((i - (2 * n - 1) * (r - 1)) % (2 * n - 1) == 0) { System.out.print("\n"); r++; } if (i++<n * (2 * n - 1)) starPattern(n); } }
8
Output
Clear
ADVERTISEMENTS