Java Program to Print a Plus (+) Pattern

Introduction

Printing a plus (+) pattern using stars (*) involves printing a cross shape, where the middle row and the middle column are filled with stars. This is a good exercise for practicing pattern generation using nested loops in Java.

Problem Statement

Create a Java program that:

  • Accepts the size of the plus pattern.
  • Prints a plus-shaped pattern using stars (*).

Example:

  • Input: 5

  • Output:

      *  
      *  
    ***** 
      *  
      *  
    
  • Input: 7

  • Output:

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

Solution Steps

  1. Take Input: Define the size of the plus pattern.
  2. Print the Plus Pattern: Use nested loops to print stars (*) in the middle row and middle column.
  3. Display the Result: Print the plus pattern on the console.

Java Program

// Java Program to Print a Plus (+) Pattern
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class PlusPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Get the size of the plus pattern
        System.out.print("Enter the size of the plus pattern (odd number): ");
        int size = scanner.nextInt();

        // Middle index calculation (since the pattern is symmetric)
        int mid = size / 2;

        // Step 2: Print the plus pattern using nested loops
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // Step 3: Print stars for the middle row or middle column
                if (i == mid || j == mid) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();  // Move to the next line after printing each row
        }

        // Close the scanner
        scanner.close();
    }
}

Explanation

Step 1: Take Input

  • The program prompts the user for the size of the plus pattern. The size must be an odd number for the pattern to be symmetric.

Step 2-3: Print the Plus Pattern

  • A nested loop structure is used to iterate through each row (i) and each column (j).
  • The condition if (i == mid || j == mid) ensures that stars (*) are printed for the middle row (i == mid) and the middle column (j == mid).
  • All other positions are filled with spaces (" ").

Output Example

For an input of 5, the program prints:

  *  
  *  
*****
  *  
  *  

For an input of 7, the program prints:

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

Example with Different Input

For an input of 9, the program outputs:

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

Demo

Java Program to Print a Plus (+) Pattern

Conclusion

This Java program demonstrates how to print a plus (+) pattern using nested loops. By controlling the placement of stars and spaces based on row and column indices, the program is able to create the symmetric plus shape. This exercise is a good way to practice loop manipulation and pattern generation in Java.

Comments