Java Code Example: Right Arrow Pattern

In this code, the user is prompted to enter the number of rows for the right arrow pattern using the Scanner class.

Code Example

import java.util.Scanner;

public class RightArrowPattern {
    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 right arrow pattern
        for (int i = 1; i <= numRows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        for (int i = numRows - 1; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Output

*
**
***
****
*****
****
***
**
*

Code Explanation

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 the desired number of asterisks (*) for each row.
The System.out.print("*"); statement prints an asterisk.
The System.out.println(); statement is used to move to the next line after each row.