Print Heart pattern
The heart pattern is a popular design that resembles the shape of a heart. This pattern is often used in various applications, such as printing on greeting cards, creating artistic designs, or as a visual representation of love and affection. In this article, we will explore how to generate a heart pattern using a simple algorithm.

Problem Statement:
The task is to write a program that prints a heart pattern of a given height. The height of the pattern determines the size and complexity of the heart. The program should take the height as input and display the corresponding heart pattern.
For example, if the input height is 5, the program should generate the following pattern:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * ** * * * * * ** * * * ** * **
Algorithm:
The algorithm for generating the heart pattern can be divided into several steps:
- Define a function to print a line of stars (*).
- Define a function to print a line of spaces.
- Define a function to print the heart pattern.
- In the main function, call the print_heart function with the desired height as a parameter.
Pseudocode:
function print_star(size):
for counter = 0 to size:
if counter is even:
print "*"
else:
print " "
function print_space(size):
for counter = 0 to size:
print " "
function print_heart(height):
if height <= 2 or height is even:
return
for i = 0 to height/2:
print_space(height - i)
print_star(height + i * 2)
print_space((height + 1) - i * 2)
print_star(height + i * 2)
for i = 1 to height:
print_space((height/2) + (i * 2))
print_star(height * 2 - (i * 2) + 1)
print_star(height * 2 - (i * 2) + 1)
function main():
print_heart(5)
Explanation:
The algorithm starts by defining two helper functions: print_star and print_space. The print_star function prints a line of stars (*), while the print_space function prints a line of spaces. These functions are used to build the heart pattern by printing the appropriate number of stars and spaces in each line.
The print_heart function takes the height of the pattern as a parameter. It first checks if the height is less than or equal to 2 or if it is an even number. In such cases, it returns, as a heart pattern cannot be generated with these inputs.
The function then uses a loop to print the top layer of the heart pattern. It starts from the first row and goes up to the height divided by 2. In each iteration, it prints the required number of spaces, stars, spaces, and stars to form the shape of the heart.
After printing the top layer, the function enters another loop to print the bottom layer of the heart pattern. This loop starts from the second row and goes up to the height. In each iteration, it prints the required number of spaces and stars to complete the heart pattern.
The main function simply calls the print_heart function with a specific height to generate and display the heart pattern.
Code Solution
Here given code implementation process.
//C Program
//Print Heart pattern
#include <stdio.h>
//Include star of given size
void print_star(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if(counter%2==0)
{
//Add Star
printf("*");
}
else
{
//Add Space
printf(" ");
}
}
}
//Display space of given size
void print_space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf(" ");
}
}
//Print Heart star pattern of given height
void print_heart(int height)
{
if(height<=2 || height%2==0)
{
return;
}
printf("\nHeight : %d \n\n",height);
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height/2; ++i)
{
print_space(height-i);
print_star(height+i*2);
print_space((height+1)-i*2);
print_star(height+i*2);
printf("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
print_space((height/2)+(i)*2);
print_star(height*2-(i)*2+1);
print_star(height*2-(i)*2+1);
printf("\n");
}
}
int main()
{
//Simple test
print_heart(5);
print_heart(7);
print_heart(9);
return 0;
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
Java Program
Print Heart pattern
*/
class MyPattern
{
//Include star of given size
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
//Add Star
System.out.print("*");
}
else
{
//Add Space
System.out.print(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
//Print Heart star pattern of given height
public void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
System.out.print("\nHeight : " + height + " \n\n");
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; ++i)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height + 1) - i * 2);
print_star(height + i * 2);
System.out.print("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
print_space((height / 2) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
print_star(height * 2 - (i) * 2 + 1);
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
C++ Program
Print Heart pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public:
//Include star of given size
void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
cout << "*";
}
else
{
cout << " ";
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
//Print Heart star pattern of given height
void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
cout << "\nHeight : " << height << " \n\n";
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; ++i)
{
this->print_space(height - i);
this->print_star(height + i * 2);
this->print_space((height + 1) - i * 2);
this->print_star(height + i * 2);
cout << "\n";
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
this->print_space((height / 2) + (i) * 2);
this->print_star(height * 2 - (i) * 2 + 1);
this->print_star(height * 2 - (i) * 2 + 1);
cout << "\n";
}
}
};
int main()
{
MyPattern obj ;
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
return 0;
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
C# Program
Print Heart pattern
*/
using System;
class MyPattern
{
//Include star of given size
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
Console.Write(" ");
}
}
//Print Heart star pattern of given height
public void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
Console.Write("\nHeight : " + height + " \n\n");
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; i++)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height + 1) - i * 2);
print_star(height + i * 2);
Console.Write("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; i++)
{
print_space((height / 2) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
print_star(height * 2 - (i) * 2 + 1);
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
<?php
/*
Php Program
Print Heart pattern
*/
class MyPattern
{
//Include star of given size
function print_star($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
if ($counter % 2 == 0)
{
echo "*";
}
else
{
echo " ";
}
}
}
//Display space of given size
function print_space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
echo " ";
}
}
//Print Heart star pattern of given height
function print_heart($height)
{
if ($height <= 2 || $height % 2 == 0)
{
return;
}
echo "\nHeight : ". $height ." \n\n";
$i = 0;
//Include top layer of heart pattern
for ($i = 0; $i <= intval($height / 2); ++$i)
{
$this->print_space($height - $i);
$this->print_star($height + $i * 2);
$this->print_space(($height + 1) - $i * 2);
$this->print_star($height + $i * 2);
echo "\n";
}
//Include bottom layer of heart pattern
for ($i = 1; $i <= $height; ++$i)
{
$this->print_space((intval($height / 2)) + ($i) * 2);
$this->print_star($height * 2 - ($i) * 2 + 1);
$this->print_star($height * 2 - ($i) * 2 + 1);
echo "\n";
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_heart(5);
$obj->print_heart(7);
$obj->print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
Node Js Program
Print Heart pattern
*/
class MyPattern
{
//Include star of given size
print_star(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
}
//Display space of given size
print_space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
process.stdout.write(" ");
}
}
//Print Heart star pattern of given height
print_heart(height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
process.stdout.write("\nHeight : " + height + " \n\n");
var i = 0;
//Include top layer of heart pattern
for (i = 0; i <= parseInt(height / 2); ++i)
{
this.print_space(height - i);
this.print_star(height + i * 2);
this.print_space((height + 1) - i * 2);
this.print_star(height + i * 2);
process.stdout.write("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
this.print_space((parseInt(height / 2)) + (i) * 2);
this.print_star(height * 2 - (i) * 2 + 1);
this.print_star(height * 2 - (i) * 2 + 1);
process.stdout.write("\n");
}
}
}
function main()
{
var obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
# Python 3 Program
# Print Heart pattern
class MyPattern :
# Include star of given size
def print_star(self, size) :
counter = 0
while (counter < size) :
if (counter % 2 == 0) :
print("*", end = "")
else :
print(" ", end = "")
counter += 1
# Display space of given size
def print_space(self, size) :
counter = 0
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Print Heart star pattern of given height
def print_heart(self, height) :
if (height <= 2 or height % 2 == 0) :
return
print("\nHeight : ", height ," \n\n", end = "")
i = 0
# Include top layer of heart pattern
while (i <= int(height / 2)) :
self.print_space(height - i)
self.print_star(height + i * 2)
self.print_space((height + 1) - i * 2)
self.print_star(height + i * 2)
print("\n", end = "")
i += 1
# Include bottom layer of heart pattern
i = 1
while (i <= height) :
self.print_space((int(height / 2)) + (i) * 2)
self.print_star(height * 2 - (i) * 2 + 1)
self.print_star(height * 2 - (i) * 2 + 1)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_heart(5)
obj.print_heart(7)
obj.print_heart(9)
if __name__ == "__main__": main()
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
# Ruby Program
# Print Heart pattern
class MyPattern
# Include star of given size
def print_star(size)
counter = 0
while (counter < size)
if (counter % 2 == 0)
# Add Star
print("*")
else
# Add Space
print(" ")
end
counter += 1
end
end
# Display space of given size
def print_space(size)
counter = 0
counter = 0
while (counter < size)
# Add space
print(" ")
counter += 1
end
end
# Print Heart star pattern of given height
def print_heart(height)
if (height <= 2 || height % 2 == 0)
return
end
print("\nHeight : ", height ," \n\n")
i = 0
# Include top layer of heart pattern
while (i <= height / 2)
self.print_space(height - i)
self.print_star(height + i * 2)
self.print_space((height + 1) - i * 2)
self.print_star(height + i * 2)
print("\n")
i += 1
end
# Include bottom layer of heart pattern
i = 1
while (i <= height)
self.print_space((height / 2) + (i) * 2)
self.print_star(height * 2 - (i) * 2 + 1)
self.print_star(height * 2 - (i) * 2 + 1)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_heart(5)
obj.print_heart(7)
obj.print_heart(9)
end
main()
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
Scala Program
Print Heart pattern
*/
class MyPattern
{
//Include star of given size
def print_star(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
if (counter % 2 == 0)
{
//Add Star
print("*");
}
else
{
//Add Space
print(" ");
}
counter += 1;
}
}
//Display space of given size
def print_space(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size)
{
//Add space
print(" ");
counter += 1;
}
}
//Print Heart star pattern of given height
def print_heart(height: Int): Unit = {
if (height <= 2 || height % 2 == 0)
{
return;
}
print("\nHeight : " + height + " \n\n");
var i: Int = 0;
//Include top layer of heart pattern
while (i <= (height / 2).toInt)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height + 1) - i * 2);
print_star(height + i * 2);
print("\n");
i += 1;
}
//Include bottom layer of heart pattern
i = 1;
while (i <= height)
{
print_space(((height / 2).toInt) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
print_star(height * 2 - (i) * 2 + 1);
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
/*
Swift Program
Print Heart pattern
*/
class MyPattern
{
//Include star of given size
func print_star(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
if (counter % 2 == 0)
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
counter += 1;
}
}
//Display space of given size
func print_space(_ size: Int)
{
var counter: Int = 0;
counter = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
//Print Heart star pattern of given height
func print_heart(_ height: Int)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
print("\nHeight : ", height ," \n\n", terminator: "");
var i: Int = 0;
//Include top layer of heart pattern
while (i <= height / 2)
{
self.print_space(height - i);
self.print_star(height + i * 2);
self.print_space((height + 1) - i * 2);
self.print_star(height + i * 2);
print("\n", terminator: "");
i += 1;
}
//Include bottom layer of heart pattern
i = 1;
while (i <= height)
{
self.print_space((height / 2) + (i) * 2);
self.print_star(height * 2 - (i) * 2 + 1);
self.print_star(height * 2 - (i) * 2 + 1);
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**
Time Complexity:
The time complexity of the given algorithm is approximately O(n^2), where n represents the height of the pattern. This is because the algorithm uses nested loops to iterate over the rows and columns of the pattern.
Second variant

//C Program
//Print Heart pattern set B
#include <stdio.h>
//Include star of given size
void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
//Add Star
printf("*");
}
else
{
//Add Space
printf(" ");
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
//Print Heart star pattern of given height
void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
printf("\nHeight : %d \n\n", height);
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; ++i)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height) - i * 2);
print_star(height + i * 2);
printf("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
print_space((height / 2) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
printf(" ");
print_star(height * 2 - (i) * 2);
printf("\n");
}
}
int main()
{
//Simple test
print_heart(5);
print_heart(7);
print_heart(9);
return 0;
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
Java Program
Print Heart pattern set B
*/
class MyPattern
{
//Include star of given size
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
//Add Star
System.out.print("*");
}
else
{
//Add Space
System.out.print(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
//Print Heart star pattern of given height
public void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
System.out.print("\nHeight : " + height + " \n\n");
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; ++i)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height) - i * 2);
print_star(height + i * 2);
System.out.print("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
print_space((height / 2) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
System.out.print(" ");
print_star(height * 2 - (i) * 2);
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
C++ Program
Print Heart pattern set B
*/
#include<iostream>
using namespace std;
class MyPattern
{
public:
//Include star of given size
void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
cout << "*";
}
else
{
cout << " ";
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
cout << " ";
}
}
//Print Heart star pattern of given height
void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
cout << "\nHeight : " << height << " \n\n";
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; ++i)
{
this->print_space(height - i);
this->print_star(height + i * 2);
this->print_space((height) - i * 2);
this->print_star(height + i * 2);
cout << "\n";
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
this->print_space((height / 2) + (i) * 2);
this->print_star(height * 2 - (i) * 2 + 1);
cout << " ";
this->print_star(height * 2 - (i) * 2);
cout << "\n";
}
}
};
int main()
{
MyPattern obj ;
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
return 0;
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
C# Program
Print Heart pattern set B
*/
using System;
class MyPattern
{
//Include star of given size
public void print_star(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
Console.Write(" ");
}
}
//Print Heart star pattern of given height
public void print_heart(int height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
Console.Write("\nHeight : " + height + " \n\n");
int i = 0;
//Include top layer of heart pattern
for (i = 0; i <= height / 2; i++)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height) - i * 2);
print_star(height + i * 2);
Console.Write("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; i++)
{
print_space((height / 2) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
Console.Write(" ");
print_star(height * 2 - (i) * 2);
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
<?php
/*
Php Program
Print Heart pattern set B
*/
class MyPattern
{
//Include star of given size
function print_star($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
if ($counter % 2 == 0)
{
echo "*";
}
else
{
echo " ";
}
}
}
//Display space of given size
function print_space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
echo " ";
}
}
//Print Heart star pattern of given height
function print_heart($height)
{
if ($height <= 2 || $height % 2 == 0)
{
return;
}
echo "\nHeight : ". $height ." \n\n";
$i = 0;
//Include top layer of heart pattern
for ($i = 0; $i <= intval($height / 2); ++$i)
{
$this->print_space($height - $i);
$this->print_star($height + $i * 2);
$this->print_space(($height) - $i * 2);
$this->print_star($height + $i * 2);
echo "\n";
}
//Include bottom layer of heart pattern
for ($i = 1; $i <= $height; ++$i)
{
$this->print_space((intval($height / 2)) + ($i) * 2);
$this->print_star($height * 2 - ($i) * 2 + 1);
echo " ";
$this->print_star($height * 2 - ($i) * 2);
echo "\n";
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_heart(5);
$obj->print_heart(7);
$obj->print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
Node Js Program
Print Heart pattern set B
*/
class MyPattern
{
//Include star of given size
print_star(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
process.stdout.write("*");
}
else
{
process.stdout.write(" ");
}
}
}
//Display space of given size
print_space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
process.stdout.write(" ");
}
}
//Print Heart star pattern of given height
print_heart(height)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
process.stdout.write("\nHeight : " + height + " \n\n");
var i = 0;
//Include top layer of heart pattern
for (i = 0; i <= parseInt(height / 2); ++i)
{
this.print_space(height - i);
this.print_star(height + i * 2);
this.print_space((height) - i * 2);
this.print_star(height + i * 2);
process.stdout.write("\n");
}
//Include bottom layer of heart pattern
for (i = 1; i <= height; ++i)
{
this.print_space((parseInt(height / 2)) + (i) * 2);
this.print_star(height * 2 - (i) * 2 + 1);
process.stdout.write(" ");
this.print_star(height * 2 - (i) * 2);
process.stdout.write("\n");
}
}
}
function main()
{
var obj = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
# Python 3 Program
# Print Heart pattern set B
class MyPattern :
# Include star of given size
def print_star(self, size) :
counter = 0
while (counter < size) :
if (counter % 2 == 0) :
print("*", end = "")
else :
print(" ", end = "")
counter += 1
# Display space of given size
def print_space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Print Heart star pattern of given height
def print_heart(self, height) :
if (height <= 2 or height % 2 == 0) :
return
print("\nHeight : ", height ," \n\n", end = "")
i = 0
# Include top layer of heart pattern
while (i <= int(height / 2)) :
self.print_space(height - i)
self.print_star(height + i * 2)
self.print_space((height) - i * 2)
self.print_star(height + i * 2)
print("\n", end = "")
i += 1
# Include bottom layer of heart pattern
i = 1
while (i <= height) :
self.print_space((int(height / 2)) + (i) * 2)
self.print_star(height * 2 - (i) * 2 + 1)
print(" ", end = "")
self.print_star(height * 2 - (i) * 2)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_heart(5)
obj.print_heart(7)
obj.print_heart(9)
if __name__ == "__main__": main()
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
# Ruby Program
# Print Heart pattern set B
class MyPattern
# Include star of given size
def print_star(size)
counter = 0
while (counter < size)
if (counter % 2 == 0)
# Add Star
print("*")
else
# Add Space
print(" ")
end
counter += 1
end
end
# Display space of given size
def print_space(size)
counter = 0
while (counter < size)
# Add space
print(" ")
counter += 1
end
end
# Print Heart star pattern of given height
def print_heart(height)
if (height <= 2 || height % 2 == 0)
return
end
print("\nHeight : ", height ," \n\n")
i = 0
# Include top layer of heart pattern
while (i <= height / 2)
self.print_space(height - i)
self.print_star(height + i * 2)
self.print_space((height) - i * 2)
self.print_star(height + i * 2)
print("\n")
i += 1
end
# Include bottom layer of heart pattern
i = 1
while (i <= height)
self.print_space((height / 2) + (i) * 2)
self.print_star(height * 2 - (i) * 2 + 1)
print(" ")
self.print_star(height * 2 - (i) * 2)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_heart(5)
obj.print_heart(7)
obj.print_heart(9)
end
main()
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
Scala Program
Print Heart pattern set B
*/
class MyPattern
{
//Include star of given size
def print_star(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
if (counter % 2 == 0)
{
//Add Star
print("*");
}
else
{
//Add Space
print(" ");
}
counter += 1;
}
}
//Display space of given size
def print_space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
//Add space
print(" ");
counter += 1;
}
}
//Print Heart star pattern of given height
def print_heart(height: Int): Unit = {
if (height <= 2 || height % 2 == 0)
{
return;
}
print("\nHeight : " + height + " \n\n");
var i: Int = 0;
//Include top layer of heart pattern
while (i <= (height / 2).toInt)
{
print_space(height - i);
print_star(height + i * 2);
print_space((height) - i * 2);
print_star(height + i * 2);
print("\n");
i += 1;
}
//Include bottom layer of heart pattern
i = 1;
while (i <= height)
{
print_space(((height / 2).toInt) + (i) * 2);
print_star(height * 2 - (i) * 2 + 1);
print(" ");
print_star(height * 2 - (i) * 2);
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
}
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
/*
Swift Program
Print Heart pattern set B
*/
class MyPattern
{
//Include star of given size
func print_star(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
if (counter % 2 == 0)
{
print("*", terminator: "");
}
else
{
print(" ", terminator: "");
}
counter += 1;
}
}
//Display space of given size
func print_space(_ size: Int)
{
var counter: Int = 0;
while (counter < size)
{
print(" ", terminator: "");
counter += 1;
}
}
//Print Heart star pattern of given height
func print_heart(_ height: Int)
{
if (height <= 2 || height % 2 == 0)
{
return;
}
print("\nHeight : ", height ," \n\n", terminator: "");
var i: Int = 0;
//Include top layer of heart pattern
while (i <= height / 2)
{
self.print_space(height - i);
self.print_star(height + i * 2);
self.print_space((height) - i * 2);
self.print_star(height + i * 2);
print("\n", terminator: "");
i += 1;
}
//Include bottom layer of heart pattern
i = 1;
while (i <= height)
{
self.print_space((height / 2) + (i) * 2);
self.print_star(height * 2 - (i) * 2 + 1);
print(" ", terminator: "");
self.print_star(height * 2 - (i) * 2);
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_heart(5);
obj.print_heart(7);
obj.print_heart(9);
}
main();
Output
Height : 5
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 7
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Height : 9
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment