How to print a star pattern without using loop in the java programming language
ADVERTISEMENTS
How to print a star pattern without using a loop in the java programming language. There are you will learn how to print the star pattern without using the loop.
Take an example to print this star pattern through a java program:
// How to print a star pattern without
// using loop in the java programming 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);
}
}
Output
-----Enter the size of the pattern-----
8
*
***
*****
*******
*********
***********
*************
***************