[Java] Plus Pattern Java Program

In this “Print Plus Pattern” – We have written Java programs to print/draw Plus 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.

Plus Pattern Example

Logic: To draw Plus Pattern through Java program, there would be one line of the vertical and horizontal. To draw vertical line just double (2*n) the inserted size and run one outer loop for each rows. Inside of this loop put condition as when i!=n, run internal loop print character if (j=n) else print space. When i=n then print 2*n characters for complete row.

This post covers following ways to print Plus Pattern:

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

Plus Pattern Java Program: For Loop

This Plus Pattern Java Program is following the below logic:

  1. The outer “for” loop first check the condition ( i<=n*2-1), if this condition is true then it check the if condition (i!=n) is true, then first inner “for” loop will be executed otherwise else part will execure for 2nd “for” loop.
  2. The first inner loop, if the condition (j==n) is true then it will display the symbol otherwise display the spaces. This loop will execute until the condition is satisfies.
  3. The second inner “for” loop will execute if the condition at the outer “for” loop is false, in the loop if condition is true display the symbol as given.
package _1_patterns.plus;

import java.util.Scanner;

public class PlusPatternForLoop {

	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);

		drawPlusPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

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

			System.out.println();
		}
	}
}

Output

Enter Pattern Size : 
9
Enter Symbol : $
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 
$$$$$$$$$$$$$$$$$
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 
        $ 

Plus Pattern Java Program: While Loop

This Plus Pattern Java Program is following the below logic:

  1. The outer “while” loop first check the condition ( i<=n*2-1), if this condition is true then it check the if condition (i!=n) is true, then first inner “while” loop will be executed otherwise else part will execute for 2nd “while” loop.
  2. The first inner “while” loop, if the condition (j==n) is true then it will displays the symbol otherwise display the spaces. This loop will execute untill the condition is satisfies.
  3. The second inner “while” loop will execute if the condition at the outer “while” loop is false, in the loop if condition is true display the symbol as given.
package _1_patterns.plus;

import java.util.Scanner;

public class PlusPatternForLoop {

	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);

		drawPlusPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

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

Output

Enter Pattern Size : 
7
Enter Symbol : #
      # 
      # 
      # 
      # 
      # 
      # 
#############
      # 
      # 
      # 
      # 
      # 
      # 

Plus Pattern Java Program: Do-While Loop

This Plus Pattern Java Program is following the below logic:

  1. The first outer “do-while” loop will execute, then checks the condition (i<=n*2-1).
  2. If condition (i!=n) is true, then first inner “do-while” loop will be executed otherwise second inner “do-while” loop will be executed.
  3. The outer “do-while” loop executes until the condition (i<=n*2-1) is false.

package _1_patterns.plus;

import java.util.Scanner;

public class PlusPatternForLoop {

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);

    drawPlusPattern(size, symbol);

    // Close Scanner
    sc.close();
}

private static void drawPlusPattern(int n, char c) {
int i = 1;
		int j;

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

Output

Enter Pattern Size : 
8
Enter Symbol : ^
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 
^^^^^^^^^^^^^^^
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 
       ^ 

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

Happy Learning!!!