Java Program to Print a Plus (+) Pattern

1. Introduction

In this tutorial, we'll be creating a Java program that prints a plus (+) pattern on the console, with the size specified by the user. This program is an excellent way to practice for loops and conditional statements, as it requires careful calculation of where to place the asterisks (*) that make up the plus sign. This type of pattern is not only visually appealing but also helps in understanding the positioning of characters on the console.

2. Program Steps

1. Import the Scanner class to read the size of the plus pattern from the user.

2. Prompt the user to enter the size, which will determine the length of the arms of the plus.

3. Use nested loops to iterate over the rows and columns, printing asterisks (*) to form the plus pattern and spaces elsewhere.

4. Compile and run the program, input the desired size, and observe the plus pattern.

3. Code Program

import java.util.Scanner; // Import Scanner class

public class PlusPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Create Scanner object
        System.out.print("Enter the size of the plus: "); // Prompt for user input
        int size = scanner.nextInt(); // Read the size from user

        int midRow = size / 2 + 1; // Calculate middle row for the plus pattern
        for(int i = 1; i <= size; i++) { // Loop for each row
            for(int j = 1; j <= size; j++) { // Loop for each column
                if(i == midRow || j == midRow) { // Check if current position is in the middle row or column
                    System.out.print("*"); // Print asterisk for the plus pattern
                } else {
                    System.out.print(" "); // Print space elsewhere
                }
            }
            System.out.println(); // New line after each row
        }
        scanner.close(); // Close the scanner
    }
}

Output:

Enter the size of the plus: 5
  *
  *
*****
  *
  *

Explanation:

1. The program starts by importing the Scanner class and prompting the user for the size of the plus pattern. This size influences the length of the plus sign's arms.

2. It calculates the middle row where the horizontal line of the plus will be printed. This is determined by size / 2 + 1, ensuring the plus is centered regardless of its size.

3. The program then enters a nested loop structure: the outer loop iterates over each row, and the inner loop iterates over each column. Within the inner loop, a conditional statement checks if the current row or column is the middle row or column (i == midRow || j == midRow). If so, an asterisk (*) is printed to form the plus sign. Otherwise, a space is printed.

4. After printing all columns for a row, a newline character is inserted to move to the next row, and this process repeats until the entire plus pattern is printed.

5. Finally, the Scanner object is closed to prevent resource leaks, adhering to best practices in handling user input.

Comments