Posted on by Kalkicode
Code Pattern

Print the Inverted heart pattern

The inverted heart pattern is a pattern consisting of heart-shaped figures that are inverted (pointing downwards) and arranged in a specific pattern. The pattern is created using asterisks (*) and spaces. This article presents a C program that generates the inverted heart pattern and explains the problem, algorithm, and pseudocode, along with the resulting output.

Problem Statement

The problem is to write a program that prints the inverted heart pattern of a given size. The size of the pattern determines the number of rows and the overall shape of the heart pattern. The pattern is formed by arranging asterisks and spaces in a specific manner to create the inverted heart shape.

For example, if the size is 5, the pattern would look like this:

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

Algorithm and Pseudocode

The following algorithm can be used to print the inverted heart pattern:

  1. Start with a check for the validity of the input size. If the size is less than or equal to 0 or if it is an even number, return.
  2. Print the top layer of the heart pattern by iterating from 0 to n, where n is the given size.
    • Print the required number of spaces to center-align the pattern.
    • Print the asterisks in a specific pattern. For the first row, print a single asterisk, and for subsequent rows, print asterisks based on the row number.
    • Move to the next line.
  3. Calculate variables k and l to control the bottom layer of the heart pattern.
  4. Print the bottom layer of the heart pattern by iterating from k to 0, where k is (n/2)-1.
    • Print the required number of spaces to center-align the pattern.
    • Print the asterisks based on the row number and the calculated variables.
    • Print the required number of spaces to separate the two halves of the pattern.
    • Print the asterisks again to complete the pattern for the row.
    • Move to the next line.
    • Increase the value of l.

The pseudocode for the algorithm is as follows:


printPattern(n):
  if n <= 0 or n is even:
    return
  for i from 0 to n:
    printSpace((n * 2) - (i * 2))
    if i == 0:
      printStar(1)
    else:
      printStar((i + 1 + i) * 2)
    move to the next line
  k = (n / 2) - 1
  l = 1
  for i from k to 0:
    printSpace(2 + l)
    printStar(n + i * 2)
    printSpace((n - 2) - i * 2)
    printStar(n + i * 2)
    move to the next line
    increase the value of l

Code Solution

//  C program for
//  Print the Inverted heart pattern
#include <stdio.h>

// Include star of given size
void printStar(int n)
{
	for (int i = 0; i < n; i++)
	{
		if (i % 2 == 0)
		{
			// Add Star
			printf("*");
		}
		else
		{
			// Add Space
			printf(" ");
		}
	}
}
// Display space of given size
void printSpace(int n)
{
	for (int i = 0; i < n; i++)
	{
		// Add space
		printf(" ");
	}
}
void printPattern(int n)
{
	if (n <= 0 || n % 2 == 0)
	{
		return;
	}
	printf("\n N : %d  \n\n", n);
	// Include top layer of heart pattern
	for (int i = 0; i < n; ++i)
	{
		printSpace((n *2) - (i *2));
		if (i == 0)
		{
			printStar(1);
		}
		else
		{
			printStar((i + 1 + i) *2);
		}
		printf("\n");
	}
	int k = (n / 2) - 1;
	int l = 1;
	// Include bottom layer of heart pattern
	for (int i = k; i >= 0; --i)
	{
		printSpace(2 + l);
		printStar(n + i *2);
		printSpace((n - 2) - i *2);
		printStar(n + i *2);
		printf("\n");
		l++;
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5);
	printPattern(9);
	printPattern(7);
	return 0;
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
/*
    Java Program
    Print the Inverted heart pattern
*/
public class Pattern
{
	// Include star of given size
	public void printStar(int n)
	{
		for (int i = 0; i < n; i++)
		{
			if (i % 2 == 0)
			{
				// Add Star
				System.out.print("*");
			}
			else
			{
				// Add Space
				System.out.print(" ");
			}
		}
	}
	// Display space of given size
	public void printSpace(int n)
	{
		for (int i = 0; i < n; i++)
		{
			// Add space
			System.out.print(" ");
		}
	}
	public void printPattern(int n)
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		System.out.print("\n N : " + n + " \n\n");
		// Include top layer of heart pattern
		for (int i = 0; i < n; ++i)
		{
			printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				printStar(1);
			}
			else
			{
				printStar((i + 1 + i) * 2);
			}
			System.out.print("\n");
		}
		int k = (n / 2) - 1;
		int l = 1;
		// Include bottom layer of heart pattern
		for (int i = k; i >= 0; --i)
		{
			printSpace(2 + l);
			printStar(n + i * 2);
			printSpace((n - 2) - i * 2);
			printStar(n + i * 2);
			System.out.print("\n");
			l++;
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
	}
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print the Inverted heart pattern
*/
class Pattern
{
	public:
		// Include star of given size
		void printStar(int n)
		{
			for (int i = 0; i < n; i++)
			{
				if (i % 2 == 0)
				{
					// Add Star
					cout << "*";
				}
				else
				{
					// Add Space
					cout << " ";
				}
			}
		}
	// Display space of given size
	void printSpace(int n)
	{
		for (int i = 0; i < n; i++)
		{
			// Add space
			cout << " ";
		}
	}
	void printPattern(int n)
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		cout << "\n N : " << n << " \n\n";
		// Include top layer of heart pattern
		for (int i = 0; i < n; ++i)
		{
			this->printSpace((n *2) - (i *2));
			if (i == 0)
			{
				this->printStar(1);
			}
			else
			{
				this->printStar((i + 1 + i) *2);
			}
			cout << "\n";
		}
		int k = (n / 2) - 1;
		int l = 1;
		// Include bottom layer of heart pattern
		for (int i = k; i >= 0; --i)
		{
			this->printSpace(2 + l);
			this->printStar(n + i *2);
			this->printSpace((n - 2) - i *2);
			this->printStar(n + i *2);
			cout << "\n";
			l++;
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(9);
	task->printPattern(7);
	return 0;
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
// Include namespace system
using System;
/*
    Csharp Program
    Print the Inverted heart pattern
*/
public class Pattern
{
	// Include star of given size
	public void printStar(int n)
	{
		for (int i = 0; i < n; i++)
		{
			if (i % 2 == 0)
			{
				// Add Star
				Console.Write("*");
			}
			else
			{
				// Add Space
				Console.Write(" ");
			}
		}
	}
	// Display space of given size
	public void printSpace(int n)
	{
		for (int i = 0; i < n; i++)
		{
			// Add space
			Console.Write(" ");
		}
	}
	public void printPattern(int n)
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		Console.Write("\n N : " + n + " \n\n");
		// Include top layer of heart pattern
		for (int i = 0; i < n; ++i)
		{
			this.printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				this.printStar(1);
			}
			else
			{
				this.printStar((i + 1 + i) * 2);
			}
			Console.Write("\n");
		}
		int k = (n / 2) - 1;
		int l = 1;
		// Include bottom layer of heart pattern
		for (int i = k; i >= 0; --i)
		{
			this.printSpace(2 + l);
			this.printStar(n + i * 2);
			this.printSpace((n - 2) - i * 2);
			this.printStar(n + i * 2);
			Console.Write("\n");
			l++;
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
	}
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
package main
import "fmt"
/*
    Go Program
    Print the Inverted heart pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
// Include star of given size
func(this Pattern) printStar(n int) {
	for i := 0 ; i < n ; i++ {
		if i % 2 == 0 {
			// Add Star
			fmt.Print("*")
		} else {
			// Add Space
			fmt.Print(" ")
		}
	}
}
// Display space of given size
func(this Pattern) printSpace(n int) {
	for i := 0 ; i < n ; i++ {
		// Add space
		fmt.Print(" ")
	}
}
func(this Pattern) printPattern(n int) {
	if n <= 0 || n % 2 == 0 {
		return
	}
	fmt.Print("\n N : ", n, " \n\n")
	// Include top layer of heart pattern
	for i := 0 ; i < n ; i++ {
		this.printSpace((n * 2) - (i * 2))
		if i == 0 {
			this.printStar(1)
		} else {
			this.printStar((i + 1 + i) * 2)
		}
		fmt.Print("\n")
	}
	var k int = (n / 2) - 1
	var l int = 1
	// Include bottom layer of heart pattern
	for i := k ; i >= 0 ; i-- {
		this.printSpace(2 + l)
		this.printStar(n + i * 2)
		this.printSpace((n - 2) - i * 2)
		this.printStar(n + i * 2)
		fmt.Print("\n")
		l++
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
<?php
/*
    Php Program
    Print the Inverted heart pattern
*/
class Pattern
{
	// Include star of given size
	public	function printStar($n)
	{
		for ($i = 0; $i < $n; $i++)
		{
			if ($i % 2 == 0)
			{
				// Add Star
				echo("*");
			}
			else
			{
				// Add Space
				echo(" ");
			}
		}
	}
	// Display space of given size
	public	function printSpace($n)
	{
		for ($i = 0; $i < $n; $i++)
		{
			// Add space
			echo(" ");
		}
	}
	public	function printPattern($n)
	{
		if ($n <= 0 || $n % 2 == 0)
		{
			return;
		}
		echo("\n N : ".$n."\n\n");
		// Include top layer of heart pattern
		for ($i = 0; $i < $n; ++$i)
		{
			$this->printSpace(($n * 2) - ($i * 2));
			if ($i == 0)
			{
				$this->printStar(1);
			}
			else
			{
				$this->printStar(($i + 1 + $i) * 2);
			}
			echo("\n");
		}
		$k = ((int)($n / 2)) - 1;
		$l = 1;
		// Include bottom layer of heart pattern
		for ($i = $k; $i >= 0; --$i)
		{
			$this->printSpace(2 + $l);
			$this->printStar($n + $i * 2);
			$this->printSpace(($n - 2) - $i * 2);
			$this->printStar($n + $i * 2);
			echo("\n");
			$l++;
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(9);
	$task->printPattern(7);
}
main();

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
/*
    Node JS Program
    Print the Inverted heart pattern
*/
class Pattern
{
	// Include star of given size
	printStar(n)
	{
		for (var i = 0; i < n; i++)
		{
			if (i % 2 == 0)
			{
				// Add Star
				process.stdout.write("*");
			}
			else
			{
				// Add Space
				process.stdout.write(" ");
			}
		}
	}
	// Display space of given size
	printSpace(n)
	{
		for (var i = 0; i < n; i++)
		{
			// Add space
			process.stdout.write(" ");
		}
	}
	printPattern(n)
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		process.stdout.write("\n N : " + n + " \n\n");
		// Include top layer of heart pattern
		for (var i = 0; i < n; ++i)
		{
			this.printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				this.printStar(1);
			}
			else
			{
				this.printStar((i + 1 + i) * 2);
			}
			process.stdout.write("\n");
		}
		var k = (parseInt(n / 2)) - 1;
		var l = 1;
		// Include bottom layer of heart pattern
		for (var i = k; i >= 0; --i)
		{
			this.printSpace(2 + l);
			this.printStar(n + i * 2);
			this.printSpace((n - 2) - i * 2);
			this.printStar(n + i * 2);
			process.stdout.write("\n");
			l++;
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
}
main();

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
#    Python 3 Program
#    Print the Inverted heart pattern
class Pattern :
	#  Include star of given size
	def printStar(self, n) :
		i = 0
		while (i < n) :
			if (i % 2 == 0) :
				#  Add Star
				print("*", end = "")
			else :
				#  Add Space
				print(" ", end = "")
			
			i += 1
		
	
	#  Display space of given size
	def printSpace(self, n) :
		i = 0
		while (i < n) :
			#  Add space
			print(" ", end = "")
			i += 1
		
	
	def printPattern(self, n) :
		if (n <= 0 or n % 2 == 0) :
			return
		
		print("\n N : ", n ," \n")
		i = 0
		#  Include top layer of heart pattern
		while (i < n) :
			self.printSpace((n * 2) - (i * 2))
			if (i == 0) :
				self.printStar(1)
			else :
				self.printStar((i + 1 + i) * 2)
			
			print(end = "\n")
			i += 1
		
		k = (int(n / 2)) - 1
		l = 1
		i = k
		#  Include bottom layer of heart pattern
		while (i >= 0) :
			self.printSpace(2 + l)
			self.printStar(n + i * 2)
			self.printSpace((n - 2) - i * 2)
			self.printStar(n + i * 2)
			print(end = "\n")
			l += 1
			i -= 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)

if __name__ == "__main__": main()

Output

 N :  5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N :  9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N :  7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
#    Ruby Program
#    Print the Inverted heart pattern
class Pattern 
	#  Include star of given size
	def printStar(n) 
		i = 0
		while (i < n) 
			if (i % 2 == 0) 
				#  Add Star
				print("*")
			else
 
				#  Add Space
				print(" ")
			end

			i += 1
		end

	end

	#  Display space of given size
	def printSpace(n) 
		i = 0
		while (i < n) 
			#  Add space
			print(" ")
			i += 1
		end

	end

	def printPattern(n) 
		if (n <= 0 || n % 2 == 0) 
			return
		end

		print("\n N : ", n ," \n\n")
		i = 0
		#  Include top layer of heart pattern
		while (i < n) 
			self.printSpace((n * 2) - (i * 2))
			if (i == 0) 
				self.printStar(1)
			else
 
				self.printStar((i + 1 + i) * 2)
			end

			print("\n")
			i += 1
		end

		k = (n / 2) - 1
		l = 1
		i = k
		#  Include bottom layer of heart pattern
		while (i >= 0) 
			self.printSpace(2 + l)
			self.printStar(n + i * 2)
			self.printSpace((n - 2) - i * 2)
			self.printStar(n + i * 2)
			print("\n")
			l += 1
			i -= 1
		end

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
end

main()

Output

 N : 5 

          *
        * * * 
      * * * * * 
    * * * * * * * 
  * * * * * * * * * 
   * * * * * * * *
    * * *   * * *

 N : 9 

                  *
                * * * 
              * * * * * 
            * * * * * * * 
          * * * * * * * * * 
        * * * * * * * * * * * 
      * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * 
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7 

              *
            * * * 
          * * * * * 
        * * * * * * * 
      * * * * * * * * * 
    * * * * * * * * * * * 
  * * * * * * * * * * * * * 
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
/*
    Scala Program
    Print the Inverted heart pattern
*/
class Pattern()
{
	// Include star of given size
	def printStar(n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			if (i % 2 == 0)
			{
				// Add Star
				print("*");
			}
			else
			{
				// Add Space
				print(" ");
			}
			i += 1;
		}
	}
	// Display space of given size
	def printSpace(n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			// Add space
			print(" ");
			i += 1;
		}
	}
	def printPattern(n: Int): Unit = {
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		print("\n N : " + n + " \n\n");
		var i: Int = 0;
		// Include top layer of heart pattern
		while (i < n)
		{
			printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				printStar(1);
			}
			else
			{
				printStar((i + 1 + i) * 2);
			}
			print("\n");
			i += 1;
		}
		var k: Int = (n / 2) - 1;
		var l: Int = 1;
		i = k;
		// Include bottom layer of heart pattern
		while (i >= 0)
		{
			printSpace(2 + l);
			printStar(n + i * 2);
			printSpace((n - 2) - i * 2);
			printStar(n + i * 2);
			print("\n");
			l += 1;
			i -= 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
	}
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
/*
    Swift 4 Program
    Print the Inverted heart pattern
*/
class Pattern
{
	// Include star of given size
	func printStar(_ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			if (i % 2 == 0)
			{
				// Add Star
				print("*", terminator: "");
			}
			else
			{
				// Add Space
				print(" ", terminator: "");
			}
			i += 1;
		}
	}
	// Display space of given size
	func printSpace(_ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			// Add space
			print(" ", terminator: "");
			i += 1;
		}
	}
	func printPattern(_ n: Int)
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		print("\n N : ", n ," \n");
		var i: Int = 0;
		// Include top layer of heart pattern
		while (i < n)
		{
			self.printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				self.printStar(1);
			}
			else
			{
				self.printStar((i + 1 + i) * 2);
			}
			print(terminator: "\n");
			i += 1;
		}
		let k: Int = (n / 2) - 1;
		var l: Int = 1;
		i = k;
		// Include bottom layer of heart pattern
		while (i >= 0)
		{
			self.printSpace(2 + l);
			self.printStar(n + i * 2);
			self.printSpace((n - 2) - i * 2);
			self.printStar(n + i * 2);
			print(terminator: "\n");
			l += 1;
			i -= 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
}
main();

Output

 N :  5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N :  9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N :  7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *
/*
    Kotlin Program
    Print the Inverted heart pattern
*/
class Pattern
{
	// Include star of given size
	fun printStar(n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			if (i % 2 == 0)
			{
				// Add Star
				print("*");
			}
			else
			{
				// Add Space
				print(" ");
			}
			i += 1;
		}
	}
	// Display space of given size
	fun printSpace(n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			// Add space
			print(" ");
			i += 1;
		}
	}
	fun printPattern(n: Int): Unit
	{
		if (n <= 0 || n % 2 == 0)
		{
			return;
		}
		print("\n N : " + n + " \n\n");
		var i: Int = 0;
		// Include top layer of heart pattern
		while (i < n)
		{
			this.printSpace((n * 2) - (i * 2));
			if (i == 0)
			{
				this.printStar(1);
			}
			else
			{
				this.printStar((i + 1 + i) * 2);
			}
			print("\n");
			i += 1;
		}
		val k: Int = (n / 2) - 1;
		var l: Int = 1;
		i = k;
		// Include bottom layer of heart pattern
		while (i >= 0)
		{
			this.printSpace(2 + l);
			this.printStar(n + i * 2);
			this.printSpace((n - 2) - i * 2);
			this.printStar(n + i * 2);
			print("\n");
			l += 1;
			i -= 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
}

Output

 N : 5

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

 N : 9

                  *
                * * *
              * * * * *
            * * * * * * *
          * * * * * * * * *
        * * * * * * * * * * *
      * * * * * * * * * * * * *
    * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * *
    * * * * * * *   * * * * * * *
     * * * * * *     * * * * * *
      * * * * *       * * * * *

 N : 7

              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
    * * * * * * * * * * *
  * * * * * * * * * * * * *
   * * * * * * * * * * * *
    * * * * *   * * * * *
     * * * *     * * * *

Resultant Output Explanation

The program generates the inverted heart pattern for three different sizes: 5, 9, and 7. Each pattern is printed separately. The output is shown below each pattern.

For example, for the size 5, the pattern is displayed as:

          *
        * * *
      * * * * *
    * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * *   * * *

The output for each size follows the same pattern. The heart shape is formed by arranging the asterisks in a specific manner, with spaces used to center-align the rows and separate the two halves of the pattern. The top layer is printed first, followed by the bottom layer.

The time complexity of the program depends on the size of the pattern, represented by 'n'. The algorithm iterates 'n' times to print the top layer and another 'n/2' times to print the bottom layer. Therefore, the time complexity can be approximated as O(n).

Comment

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