Print Heart pattern
Given a height of Odd numbers, Our goal is to display a simple heart pattern of this height. Let see an example

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
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * ** * * * * * * * *
* * * * * * * ** * * * * * * *
* * * * * * ** * * * * * *
* * * * * ** * * * * *
* * * * ** * * * *
* * * ** * * *
* * ** * *
* ** *
**

//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