×

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

How to Convert String to Date in Java

How to Convert String to Date in java You can convert String to Date in Java by using the parse() method. There are two classes which have parse() method namely, DateFormat and SimpleDateFormat classes....

2 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.