Right Arrow Stars | Java Patterns

Total Views : 51
Zoom In Zoom Out Read Later Print

In this blog post we will write code in java to print stars in right arrow pattern using 3 different way. At the end of the post, we have added project so that you can download and execute the below codes.

1. Using While Loop.
2. Using Do - While Loop.
3. Using For Loop.

1. Using While Loop

While Loop

package com.manjeet.prodevsblog.stars;
import java.util.Scanner;

public class RightArrowPattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter N : ");
        int n = sc.nextInt();

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

        printDivider(n);

        int i = 0, j;
        while(i < n) {
            j = 0;
            while(j < n) {
                if(j < i) {
                    System.out.print("  ");
                } else {
                    System.out.print(chrToPrint);
                }
                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(chrToPrint);
                }
                j++;
            }
            System.out.println();
            i++;
        }

    }
    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
        //System.out.println();
    }
}

Output:

Enter N : 
6
Enter Symbol To Use : *
---------------
******
  *****
    ****
      ***
        **
          *
        **
      ***
    ****
  *****
******

Process finished with exit code 0

1. While loop first checks if the condition is true or not, if (user entered more then 0) true then it executes the code inside it.

2. The first outer while loop will display half of the pattern (or upper part of it ) and the second outer loop will display next half of the pattern(lower half).

3. If the condition of the first outer while loop is true, then it comes to the inner loop of first one, the inner loop condition also true then checks the if condition, "if" condition is true, then it displays space otherwise will display character "*". The inner loop will execute the code until condition false and continue with the outer loop. The first outer loop executes the code until the condition i < n.

4. After first loop execution the cursor comes to next line then second outer while loop will be executed until the condition false same as the first loop but it print enter reverse of character printed by the first loop.

Using Do-While Loop

Do While Loop

package com.manjeet.prodevsblog.stars;
import java.util.Scanner;

public class RightArrowPattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter N : ");
        int n = sc.nextInt();

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

        printDivider(n);

        int i = 0, j;
        do {
            j = 0;
            do {
                if(j < i) {
                    System.out.print("  ");
                } else {
                    System.out.print(chrToPrint);
                }
                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(chrToPrint);
                }
                j++;
            } while(j < n);
            System.out.println();
            i++;
        } while(i <= n);

    }
    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
        //System.out.println();
    }
}

Output:

Enter N : 
5
Enter Symbol To Use : >
-------------
>>>>>
  >>>>
    >>>
      >>
        >
      >>
    >>>
  >>>>
>>>>>

Process finished with exit code 0

1) In the first outer do-while loop, it executes the code first and then checks the condition i < n. The first outer do-while loop will run until the condition i < n false. It will display the first half of the pattern (upper half).

2) The second outer do-while loop will execute the code until the condition i < n is false. It will display the second half of the pattern (lower).

Using For Loop

For Loop

package com.manjeet.prodevsblog.stars;
import java.util.Scanner;

public class RightArrowPattern {
    private static final char DIVIDER = '-' ;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter N : ");
        int n = sc.nextInt();

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

        printDivider(n);

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(j < i)
                    System.out.print("  ");
                else
                    System.out.print(chrToPrint);
            }

            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(chrToPrint);
            }
            System.out.println();
        }

    }
    static void printDivider(int n) {
        System.out.println(String.valueOf(DIVIDER).repeat((n * 2) + 3));
        //System.out.println();
    }
}

Output:

Enter N : 
8
Enter Symbol To Use : M
-------------------
MMMMMMMM
  MMMMMMM
    MMMMMM
      MMMMM
        MMMM
          MMM
            MM
              M
            MM
          MMM
        MMMM
      MMMMM
    MMMMMM
  MMMMMMM
MMMMMMMM

Process finished with exit code 0

1. In this way outer for loop is for rows and the inner loops for columns.

2. The first outer for loop displays the half of the pattern (upper half) and second outer for loop displays the next half of the pattern (lower half).

3. In first outer loop the condition will be checked if it is true then it checks the inner for loop condition if it is true then checks the "if" condition. if condition is true in inner for loop then displays space otherwise displays character which we have given by user using console to display.

4. The inner for loop executes the code until the inner condition is false. The first outer loop will display this pattern of upper half.

5. After fist loop is finished the execution then cursor comes to next line and the second outer loop will be executed.

6. The condition at second outer loop is true, then inner loop will be executed until the condition false of the inner loop.

7. In inner loop if "if" condition is true, then it displays space otherwise it displays character  which we have given by user using console to display. The code in the Outer loop will be executed until the condition is false. The second outer for loop will display lower half of arrow.

You can download the Complete project from below link.

See More

Latest Photos