×

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it.

Method for Printing the Heart Number Pattern

  • Put the value of the total row into the integer variable row.
  • Put the column values into an inner loop to print them.
  • Start by printing the left semi-circle using a nested for loop.
  • Use a nested for loop to start printing the right semi-circle.
  • To print an inverted pyramid beneath the semi-circles, use another nested loop.

Program 1: Print heart pattern in java

import java.io.*;
import java.util.*;
public class HeartPattern
{
  public static void main(String[]args)
  { 
for(int i=1; i<=2; i++)
     {
for(int k=1; k<=2-i; k++)
         {
System.out.print(" ");
         } 
for(int j=1; j<=2*i; j++)
         {
System.out.print("*");
         } 
for(int k=1; k<=2*(2-i+1)-1; k++)
         {
System.out.print(" ");
         } 
for(int j=1; j<=2*i; j++)
         {
System.out.print("*");
         } 
System.out.println();
     }
for(int i=1; i<=5; i++)
     {
for(int k=1; k<=i-1; k++)
         {
System.out.print(" ");
         } 
for(int j=1; j<=2*(5-i+1)-1; j++)
         {
System.out.print("*");
         } 
System.out.println();
     }
  } 
}

The output of the above program

Heart Pattern in Java

Program 2: To print heart pattern in java

import java.io.*;
import java.util.*;
public class HeartPattern
{
  public static void main(String[] args) 
    {
        int r,c, row;
        Scanner sc= new Scanner(System.in); 
        System.out.print("Enter no of rows = ");
        row=sc.nextInt();
for(r = row/2; r <= row; r+=2)
        { 
for(c = 1; c < row-r; c+=2) 
            {  
System.out.print(" ");  
            }
for(c = 1; c <= r; c++)
            {  
System.out.print(r);  
            }
for(c = 1; c <= row-r; c++)
            {  
System.out.print(" ");  
            }  
for(c = 1; c <= r; c++)
            {  
System.out.print(r);  
            } 
System.out.println("");  
        }  


for(r = row; r >= 1; r--)
        {
for(c = r; c < row; c++)
            {  
System.out.print(" ");  
            } 


for(c = 1; c <= (r*2)-1; c++)
            {  
System.out.print(r);  
            }  


System.out.println("");  
        }  


    } 
}

The output of the above program

Heart Pattern in Java

Related Topics

Balanced Parentheses in Java

One of the frequent programming issues, commonly referred to as the Balanced brackets issue, is the problem with the balanced parenthesis. Interviewers frequently provide this task, in which we must...

5 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java BLOB

The two data types used in Java to store binary and big-character objects are BLOB and CLOB. In contrast to other types of data like float, int, double, etc., it...

4 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.