Introduction
Printing a heart shape using stars (*
) is an interesting programming exercise. The heart pattern is a bit more complex compared to regular shapes like pyramids or X
patterns, as it requires carefully positioning stars and spaces based on both row and column indices. This guide will walk you through writing a Java program that prints a heart-shaped pattern using stars.
Problem Statement
Create a Java program that:
- Accepts the size of the heart pattern.
- Prints a heart-shaped pattern using stars (
*
).
Example:
- Input:
size = 6
(a larger heart shape) - Output:
** ** **** **** *********** ********* ******* ***** *** *
Solution Steps
- Set the Heart Pattern Dimensions: Pre-define the dimensions of the heart shape based on the size input.
- Use Nested Loops: Use nested loops to print the stars (
*
) and spaces based on the shape of the heart. - Display the Heart Shape: Print the heart pattern on the console.
Java Program
// Java Program to Print a Heart Shape Star Pattern
// Author: https://www.rameshfadatare.com/
public class HeartShape {
public static void main(String[] args) {
// Step 1: Define the size (height) of the heart
int size = 6;
// Step 2: Upper part of the heart
for (int i = size / 2; i <= size; i += 2) {
// Step 3: Print spaces before the first set of stars
for (int j = 1; j < size - i; j += 2) {
System.out.print(" ");
}
// Step 4: Print the first set of stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Step 5: Print spaces between the two sets of stars
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// Step 6: Print the second set of stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Step 7: Lower part of the heart (inverted triangle)
for (int i = size; i >= 1; i--) {
// Step 8: Print spaces before the stars
for (int j = i; j < size; j++) {
System.out.print(" ");
}
// Step 9: Print the stars in each row
for (int j = 1; j <= (i * 2) - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Explanation
Step 1: Define the Size
- The
size
variable controls the overall size of the heart shape. This determines the number of rows for the upper and lower parts of the heart.
Step 2-6: Print the Upper Part of the Heart
- The upper part of the heart consists of two halves. The outer loop controls the rows, and nested loops handle the stars and spaces.
- The first set of stars is printed, followed by spaces, and then another set of stars, to form the two lobes of the heart.
Step 7-9: Print the Lower Part of the Heart
- The lower part of the heart is an inverted triangle. The outer loop controls the number of rows, and nested loops handle the spaces and stars for each row.
Output Example
For size = 6
, the program prints:
** **
**** ****
***********
*********
*******
*****
***
*
Example with Different Size
For size = 8
, the heart shape will be larger and fuller:
** **
**** ****
****** ******
******** ********
*****************
***************
*************
***********
*********
*******
*****
***
*
Conclusion
This Java program demonstrates how to print a heart-shaped star pattern using nested loops. By carefully controlling the placement of stars and spaces, the program creates the heart shape. This exercise is a great way to practice pattern generation and loop manipulation in Java.
Comments
Post a Comment
Leave Comment