Print the Inverted heart pattern

Here given code implementation process.

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

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


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







© 2021, kalkicode.com, All rights reserved