In this code, the user is prompted to enter the number of rows for the left arrow pattern using the Scanner
class. The first for
loop is responsible for printing the top half of the pattern, and the second for
loop prints the bottom half. The inner for
loops within each outer loop are used to print spaces and asterisks (*) for each row. The System.out.print(" ");
statement prints a space, and the System.out.print("*");
statement prints an asterisk. The System.out.println();
statement is used to move to the next line after each row.
import java.util.Scanner;
public class LeftArrowPattern {
public static void main(String[] args) {
// Get the number of rows from the user
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int numRows = input.nextInt();
// Display the left arrow pattern
for (int i = 1; i <= numRows; i++) {
for (int j = 1; j <= numRows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = numRows - 1; i >= 1; i--) {
for (int j = 1; j <= numRows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
**
***
****
*****
****
***
**
*