[Java] Right Arrow Pattern Java Program

In this “Print Right Arrow Pattern” – We have written Java programs to print/draw Right Arrow Pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

Right Arrow Pattern Example

This post covers following ways to print Star (X) Pattern:

  • Print Right Arrow Pattern Java Program: Using for Loop
  • Print Right Arrow Pattern Java Program: Using While Loop
  • Print Right Arrow Pattern Java Program: Using Do-While Loop

Logic: This Right Arrow Pattern logic is implemented mainly in two parts; first outer loop will display the first half of the pattern and second outer loop will display the second half of the pattern. Here outer loop will display the rows and inner loops will display the columns.

Right Arrow Pattern Java Program: For Loop

This Right Arrow Pattern Java Program is following the below logic:

  1. First outer for loop condition will checked if it is true then it checks the inner for loop condition if it is true then display space otherwise display symbol which user have given to display.
  2. Inner for loop will execute the code until the condition is false.
  3. Similarly, the condition at 2nd outer for loop is true, then inner for loop will execute until the condition id false. In inner loop “if” condition is true, then it display space otherwise displays symbol which user have given to display.
package _1_patterns.right_arrow;

import java.util.Scanner;

public class RightArrowPatternForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawRightArrowPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawRightArrowPattern(int n, char c) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (j < i)
					System.out.print("  ");
				else
					System.out.print(c);
			}
			System.out.println();
		}
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j < n; j++) {
				if (j < n - i)
					System.out.print("  ");
				else
					System.out.print(c);
			}
			System.out.println();
		}	
	}
}

Output

Enter Pattern Size : 
5
Enter Symbol : $
$$$$$
  $$$$
    $$$
      $$
        $
      $$
    $$$
  $$$$
$$$$$

Right Arrow Pattern Java Program: While Loop

This Right Arrow Pattern Java Program is following the below logic:

  1. The condition at first outer while loop is true, then it comes to the inner loop, the inner loop condition also true then checks the “if” condition, is true, then it displays space otherwise will display symbol. The inner loop will execute the code until condition is false. The 1st outer loop executes the code until the condition is i<n.
  2. Similarly, pointer comes to the next line then second outer while loop will be executed until the condition is false.
package _1_patterns.rightarrow;

import java.util.Scanner;

public class RightArrowPatternWhileLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawRightArrowPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawRightArrowPattern(int n, char c) {
		int i = 0;
		int j;
		while (i < n) {
			j = 0;
			while (j < n) {
				if (j < i)
					System.out.print("  ");
				else
					System.out.print(c);
				j++;
			}
			System.out.println();
			i++;
		}
		i = 2;
		while (i <= n) {
			j = 0;
			while (j < n) {
				if (j < n - i)
					System.out.print("  ");
				else
					System.out.print(c);
				j++;
			}
			System.out.println();
			i++;
		}
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : %
%%%%%%%%
  %%%%%%%
    %%%%%%
      %%%%%
        %%%%
          %%%
            %%
              %
            %%
          %%%
        %%%%
      %%%%%
    %%%%%%
  %%%%%%%
%%%%%%%%

Right Arrow Pattern Java Program: Do-While Loop

This Java Program is following the below logic:

  1. in the first outer do-while loop, it executes the code and then checks the condition i<n, The first outer do-while loop will exceute the code until the condition i<n false.
  2. Similarly, the second outer do-while loop will execute the code until the condition i<n is false.

package _1_patterns.rightarrow;

import java.util.Scanner;

public class RightArrowPatternDoWhileLoop {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter Pattern Size : ");
    int size = sc.nextInt();

    System.out.print("Enter Symbol : ");
    char symbol = sc.next().charAt(0);

    drawRightArrowPattern(size, symbol);

    // Close Scanner
    sc.close();
}

private static void drawRightArrowPattern(int n, char c) {
int i = 0;
		int j;
		do {
			j = 0;

			do {
				if (j < i)
					System.out.print("  ");
				else
					System.out.print(c);
				j++;
			} while (j < n);

			System.out.println();
			i++;

		} while (i < n);
		i = 2;
		do {
			j = 0;
			do {
				if (j < n - i)
					System.out.print("  ");
				else
					System.out.print(c);
				j++;

			} while (j < n);

			System.out.println();
			i++;
		} while (i <= n);
}
}

Output

Enter Pattern Size : 
8
Enter Symbol : #
########
  #######
    ######
      #####
        ####
          ###
            ##
              #
            ##
          ###
        ####
      #####
    ######
  #######
########

Hope this post helps you to implement the Right Arrow Pattern through Java Program. Please share your comments.

Happy Learning!!!