Java Program to Print a Butterfly Pattern

1. Introduction

In this tutorial, we'll be creating a Java program to print a butterfly pattern on the console, using user input to determine the size of the butterfly. This pattern is an excellent example of using nested loops and conditional statements in Java to produce complex shapes. The butterfly pattern consists of two symmetrical halves, and it’s a fun way to practice programming concepts while creating something visually appealing.

2. Program Steps

1. Import the Scanner class for reading the size of the butterfly from the user.

2. Prompt the user to enter the size, which will dictate the number of rows in each half of the butterfly.

3. Use nested loops to print the upper half of the butterfly, consisting of asterisks and spaces.

4. Repeat a similar set of loops to print the lower half of the butterfly, inverting the pattern of the upper half.

5. Compile and execute the program, then input the desired size to see the butterfly pattern.

3. Code Program

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

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

        // Print the upper half of the butterfly
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= i; j++) { // Print left wing
                System.out.print("*");
            }
            for (int j = 1; j <= 2 * (size - i); j++) { // Print space between wings
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) { // Print right wing
                System.out.print("*");
            }
            System.out.println();
        }

        // Print the lower half of the butterfly
        for (int i = size; i >= 1; i--) {
            for (int j = 1; j <= i; j++) { // Print left wing
                System.out.print("*");
            }
            for (int j = 1; j <= 2 * (size - i); j++) { // Print space between wings
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) { // Print right wing
                System.out.print("*");
            }
            System.out.println();
        }
        scanner.close(); // Close the scanner
    }
}

Output:

(For size = 4)
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *

Explanation:

1. The program starts by importing the Scanner class and prompting the user to input the size of the butterfly. This size determines the number of rows in each half of the butterfly pattern.

2. For the upper half of the butterfly, the program uses a for loop that iterates from 1 to the input size. Within this loop, three nested loops print the left wing (asterisks), the space between the wings, and the right wing (asterisks) of the butterfly.

3. The pattern for the lower half of the butterfly is generated by a for loop that iterates in reverse, from the input size down to 1. Similar to the upper half, nested loops within this loop print the left wing, the space, and the right wing for each row, but in reverse order to create the bottom half of the butterfly.

4. After completing the pattern for each row, a newline character is printed to move to the next row, continuing the process until the entire butterfly pattern is formed.

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

Comments