Project
This Java program generates a heart pattern made of asterisks (*
). Here’s a breakdown of how it works:
-
Input Handling:
- The program prompts the user to enter a value (
n
), which determines the size of the heart pattern.
- The program prompts the user to enter a value (
-
First Loop (Top Part of the Heart):
- The outer loop (
for (i = n / 2; i <= n; i += 2)
) generates the top part of the heart. - Inside the loop:
- Spaces Before the First Half of the Heart:
for (j = 1; j < n - i; j += 2)
- Asterisks for the First Half of the Heart:
for (j = 1; j <= i; j++)
- Spaces Between the Two Halves:
for (j = 1; j <= n - i; j++)
- Asterisks for the Second Half of the Heart:
for (j = 1; j <= i; j++)
- Spaces Before the First Half of the Heart:
- The outer loop (
-
Second Loop (Bottom Part of the Heart):
- The loop (
for (i = n; i >= 1; i--)
) generates the lower triangular part of the heart. - Inside the loop:
- Spaces Before the Triangular Part:
for (j = i; j < n; j++)
- Asterisks for the Triangular Part:
for (j = 1; j <= (i * 2) - 1; j++)
- Spaces Before the Triangular Part:
- The loop (
-
Output Example: If the user enters
n = 6
, the output will look like this:
** **
**** ****
****** ******
***********
*********
*******
*****
***
*
- You can adjust
n
to increase or decrease the size of the heart. - This pattern works best with even values of
n
to maintain symmetry.