Introduction
A butterfly pattern is a fun and visually appealing pattern that can be printed using stars (*
). The pattern is symmetrical, with the left and right wings of the butterfly expanding outwards, then shrinking back inwards. The butterfly pattern is a good exercise for learning how to use loops for pattern printing in Java.
Problem Statement
Create a Java program that:
- Accepts the size of the butterfly pattern.
- Prints the butterfly-shaped pattern using stars (
*
).
Example:
- Input:
5
- Output:
* * ** ** *** *** **** **** ********** ********** **** **** *** *** ** ** * *
Solution Steps
- Take Input: Define the size of the butterfly pattern.
- Print the Upper Half: Use nested loops to print the upper half of the butterfly, expanding outwards.
- Print the Lower Half: Use nested loops to print the lower half of the butterfly, shrinking back inwards.
- Display the Result: Print the butterfly pattern on the console.
Java Program
// Java Program to Print a Butterfly Pattern
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class ButterflyPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the butterfly pattern
System.out.print("Enter the size of the butterfly pattern: ");
int n = scanner.nextInt();
// Step 2: Print the upper half of the butterfly pattern
for (int i = 1; i <= n; i++) {
// Step 3: Print the left wing of the butterfly
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Step 4: Print spaces in the middle
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Step 5: Print the right wing of the butterfly
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Step 6: Print the lower half of the butterfly pattern
for (int i = n; i >= 1; i--) {
// Step 7: Print the left wing of the butterfly
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Step 8: Print spaces in the middle
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Step 9: Print the right wing of the butterfly
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Close the scanner
scanner.close();
}
}
Explanation
Step 1: Take Input
- The program prompts the user for the size of the butterfly pattern. This size will determine the number of rows for the upper and lower halves of the butterfly.
Step 2-5: Print the Upper Half of the Butterfly
- The upper half of the butterfly is printed using a
for
loop that iterates from 1 ton
(the number of rows). - The first inner loop prints stars (
*
) on the left side of the butterfly. - The second inner loop prints spaces (
" "
) in the middle of the butterfly. - The third inner loop prints stars (
*
) on the right side of the butterfly.
Step 6-9: Print the Lower Half of the Butterfly
- The lower half of the butterfly is printed using a
for
loop that iterates fromn
down to 1. - The process is similar to the upper half but in reverse, shrinking the number of stars as the rows move downward.
Output Example
For an input of 5
, the program prints:
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *
Example with Different Input
For an input of 4
, the program outputs:
* *
** **
*** ***
********
********
*** ***
** **
* *
Conclusion
This Java program demonstrates how to print a butterfly-shaped star pattern using nested loops. The program is a great way to practice loop manipulation, pattern generation, and understanding symmetry in programming. By controlling the placement of stars and spaces, the program creates the butterfly shape efficiently.
Comments
Post a Comment
Leave Comment