Heart Shape Pattern using Java
In this project, we will learn how to print the heart shape patterns using the Java programming language.
-1618393199-591.png)
We will learn how to print the heart shape(as shown in the image) using asterisks. The shape and size of the pattern will depend upon the user input. We will print the pattern using for loops.
import java.util.*;
class heart
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter element : ");
int n=sc.nextInt();
for(int i=3;i<=n;i++)
{
for(int j=n-i;j>=1;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("*");
}
for(int k=2;k<=i;k++)
{
System.out.print("*");
}
for(int j=(n-i)*2;j>=1;j--)
{
System.out.print(" ");
}
for(int k=1;k<=i;k++)
{
System.out.print("*");
}
for(int k=2;k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
for(int i=1;i<=(n*2)-1;i++)
{
for(int j=2;j<=i;j++)
{
System.out.print(" ");
}
for(int k=(n*4)-1;k>=i*2;k--)
{
System.out.print("*");
}
System.out.println("");
}
}
}
Input: 5
Output:
-1618393199-591.png)
Project Files
/
Loading...
| .. | ||
| This directory is empty. | ||