Print double headed arrow pattern

Here given code implementation process.

//C Program 
//Print double headed arrow pattern
#include <stdio.h>

#include <stdlib.h>

/*Include Space of given size*/
void space(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		//Add space
		printf(" ");
	}
}
/*Include star of given size*/
void print_star(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		//Add star
		printf("*");
	}
}
//Display the double arrow of given size
void double_arrow(int size)
{
	printf("\n Size : %d \n", size);
	int i = 0;
	//print upper half layers
	for (i = 0; i < size; i++)
	{
		//include initial space
		space((size *2) - (i *2));
		//print star
		print_star(i);
		if (i >= 2)
		{
			space((i - 1) *2 - 1);
			print_star(i);
		}
		printf("\n");
	}
	//print bottom half layers
	for (i = 0; i < size; i++)
	{
		//include initial space
		space(i *2);
		//print star
		print_star(size - i);
		if (i + 1 != size)
		{
			space(((size - 1) *2) - (i *2) - 1);
			print_star(size - i);
		}
		printf("\n");
	}
}
int main()
{
	//Test Cases
	double_arrow(3);
	double_arrow(4);
	double_arrow(7);
	double_arrow(9);
	return 0;
}

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  Java Program
  Print double headed arrow pattern
*/
class MyPattern
{
	public void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			System.out.print(" ");
		}
	}
	public void print_star(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add star
			System.out.print("*");
		}
	}
	//Display the double arrow of given size
	public void double_arrow(int size)
	{
		System.out.print("\n Size : "+size+" \n");
		int i = 0;
		//print upper half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			space((size *2) - (i *2));
			//print star
			print_star(i);
			if (i >= 2)
			{
				space((i - 1) *2 - 1);
				print_star(i);
			}
			System.out.print("\n");
		}
		//print bottom half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			space(i *2);
			//print star
			print_star(size - i);
			if (i + 1 != size)
			{
				space(((size - 1) *2) - (i *2) - 1);
				print_star(size - i);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test evaluation
        obj.double_arrow(3);
        obj.double_arrow(4);
        obj.double_arrow(7);
        obj.double_arrow(9);
	}
}

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  C++ Program
  Print double headed arrow pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public: void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << " ";
		}
	}
	void print_star(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << "*";
		}
	}
	//Display the double arrow of given size
	void double_arrow(int size)
	{
		cout << "\n Size : " << size << " \n";
		int i = 0;
		//print upper half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			this->space((size * 2) - (i * 2));
			//print star
			this->print_star(i);
			if (i >= 2)
			{
				this->space((i - 1) * 2 - 1);
				this->print_star(i);
			}
			cout << "\n";
		}
		//print bottom half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			this->space(i * 2);
			//print star
			this->print_star(size - i);
			if (i + 1 != size)
			{
				this->space(((size - 1) * 2) - (i * 2) - 1);
				this->print_star(size - i);
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern  obj =  MyPattern();
	//Test evaluation
	obj.double_arrow(3);
	obj.double_arrow(4);
	obj.double_arrow(7);
	obj.double_arrow(9);
	return 0;
}

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  C# Program
  Print double headed arrow pattern
*/
using System;
class MyPattern
{
	public void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			Console.Write(" ");
		}
	}
	public void print_star(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add star
			Console.Write("*");
		}
	}
	//Display the double arrow of given size
	public void double_arrow(int size)
	{
		Console.Write("\n Size : " + size + " \n");
		int i = 0;
		//print upper half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			space((size * 2) - (i * 2));
			//print star
			print_star(i);
			if (i >= 2)
			{
				space((i - 1) * 2 - 1);
				print_star(i);
			}
			Console.Write("\n");
		}
		//print bottom half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			space(i * 2);
			//print star
			print_star(size - i);
			if (i + 1 != size)
			{
				space(((size - 1) * 2) - (i * 2) - 1);
				print_star(size - i);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test evaluation
		obj.double_arrow(3);
		obj.double_arrow(4);
		obj.double_arrow(7);
		obj.double_arrow(9);
	}
}

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
<?php
/*
  Php Program
  Print double headed arrow pattern
*/
class MyPattern
{
	public	function space($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			//Add space
			echo(" ");
		}
	}
	public	function print_star($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			//Add star
			echo("*");
		}
	}
	//Display the double arrow of given size
	public	function double_arrow($size)
	{
		echo("\n Size : ". $size ." \n");
		$i = 0;
		//print upper half layers
		for ($i = 0; $i < $size; $i++)
		{
			//include initial space
			$this->space(($size * 2) - ($i * 2));
			//print star
			$this->print_star($i);
			if ($i >= 2)
			{
				$this->space(($i - 1) * 2 - 1);
				$this->print_star($i);
			}
			echo("\n");
		}
		//print bottom half layers
		for ($i = 0; $i < $size; $i++)
		{
			//include initial space
			$this->space($i * 2);
			//print star
			$this->print_star($size - $i);
			if ($i + 1 != $size)
			{
				$this->space((($size - 1) * 2) - ($i * 2) - 1);
				$this->print_star($size - $i);
			}
			echo("\n");
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Test evaluation
	$obj->double_arrow(3);
	$obj->double_arrow(4);
	$obj->double_arrow(7);
	$obj->double_arrow(9);
}
main();

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  Node Js Program
  Print double headed arrow pattern
*/
class MyPattern
{
	space(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			process.stdout.write(" ");
		}
	}
	print_star(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add star
			process.stdout.write("*");
		}
	}
	//Display the double arrow of given size
	double_arrow(size)
	{
		process.stdout.write("\n Size : " + size + " \n");
		var i = 0;
		//print upper half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			this.space((size * 2) - (i * 2));
			//print star
			this.print_star(i);
			if (i >= 2)
			{
				this.space((i - 1) * 2 - 1);
				this.print_star(i);
			}
			process.stdout.write("\n");
		}
		//print bottom half layers
		for (i = 0; i < size; i++)
		{
			//include initial space
			this.space(i * 2);
			//print star
			this.print_star(size - i);
			if (i + 1 != size)
			{
				this.space(((size - 1) * 2) - (i * 2) - 1);
				this.print_star(size - i);
			}
			process.stdout.write("\n");
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test evaluation
	obj.double_arrow(3);
	obj.double_arrow(4);
	obj.double_arrow(7);
	obj.double_arrow(9);
}
main();

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
# Python 3 Program
# Print double headed arrow pattern

class MyPattern :
	def space(self, size) :
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	def print_star(self, size) :
		counter = 0
		while (counter < size) :
			print("*", end = "")
			counter += 1
		
	
	# Display the double arrow of given size
	def double_arrow(self, size) :
		print("\n Size : ", size )
		i = 0
		# print upper half layers
		while (i < size) :
			# include initial space
			self.space((size * 2) - (i * 2))
			# print star
			self.print_star(i)
			if (i >= 2) :
				self.space((i - 1) * 2 - 1)
				self.print_star(i)
			
			print(end = "\n")
			i += 1
		
		# print bottom half layers
		i = 0
		while (i < size) :
			# include initial space
			self.space(i * 2)
			# print star
			self.print_star(size - i)
			if (i + 1 != size) :
				self.space(((size - 1) * 2) - (i * 2) - 1)
				self.print_star(size - i)
			
			print(end = "\n")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Test evaluation
	obj.double_arrow(3)
	obj.double_arrow(4)
	obj.double_arrow(7)
	obj.double_arrow(9)


if __name__ == "__main__": main()

Output

 Size :  3

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

 Size :  4

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

 Size :  7

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

 Size :  9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
# Ruby Program
# Print double headed arrow pattern

class MyPattern

	def space(size)
	
		counter = 0
		while (counter < size)
		
			print(" ")
			counter += 1
		end
	end
	def print_star(size)
	
		counter = 0
		while (counter < size)
		
			print("*")
			counter += 1
		end
	end
	# Display the double arrow of given size
	def double_arrow(size)
	
		print("\n Size : ", size ," \n")
		i = 0
		# print upper half layers
		while (i < size)
		
			# include initial space
			self.space((size * 2) - (i * 2))
			# print star
			self.print_star(i)
			if (i >= 2)
			
				self.space((i - 1) * 2 - 1)
				self.print_star(i)
			end
			print("\n")
			i += 1
		end
		# print bottom half layers
		i = 0
		while (i < size)
		
			# include initial space
			self.space(i * 2)
			# print star
			self.print_star(size - i)
			if (i + 1 != size)
			
				self.space(((size - 1) * 2) - (i * 2) - 1)
				self.print_star(size - i)
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Test evaluation
	obj.double_arrow(3)
	obj.double_arrow(4)
	obj.double_arrow(7)
	obj.double_arrow(9)
end
main()

Output

 Size : 3 
      
    *
  ** **
***   ***
  ** **
    *

 Size : 4 
        
      *
    ** **
  ***   ***
****     ****
  ***   ***
    ** **
      *

 Size : 7 
              
            *
          ** **
        ***   ***
      ****     ****
    *****       *****
  ******         ******
*******           *******
  ******         ******
    *****       *****
      ****     ****
        ***   ***
          ** **
            *

 Size : 9 
                  
                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  Scala Program
  Print double headed arrow pattern
*/
class MyPattern
{
	def space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ");
			counter += 1;
		}
	}
	def print_star(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print("*");
			counter += 1;
		}
	}
	//Display the double arrow of given size
	def double_arrow(size: Int): Unit = {
		print("\n Size : " + size + " \n");
		var i: Int = 0;
		//print upper half layers
		while (i < size)
		{
			//include initial space
			space((size * 2) - (i * 2));
			//print star
			print_star(i);
			if (i >= 2)
			{
				space((i - 1) * 2 - 1);
				print_star(i);
			}
			print("\n");
			i += 1;
		}
		//print bottom half layers
		i = 0;
		while (i < size)
		{
			//include initial space
			space(i * 2);
			//print star
			print_star(size - i);
			if (i + 1 != size)
			{
				space(((size - 1) * 2) - (i * 2) - 1);
				print_star(size - i);
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test evaluation
		obj.double_arrow(3);
		obj.double_arrow(4);
		obj.double_arrow(7);
		obj.double_arrow(9);
	}
}

Output

 Size : 3

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

 Size : 4

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

 Size : 7

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

 Size : 9

                *
              ** **
            ***   ***
          ****     ****
        *****       *****
      ******         ******
    *******           *******
  ********             ********
*********               *********
  ********             ********
    *******           *******
      ******         ******
        *****       *****
          ****     ****
            ***   ***
              ** **
                *
/*
  Swift Program
  Print double headed arrow pattern
*/
class MyPattern
{
	func space(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ", terminator: "");
			counter += 1;
		}
	}
	func print_star(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print("*", terminator: "");
			counter += 1;
		}
	}
	//Display the double arrow of given size
	func double_arrow(_ size: Int)
	{
		print("\n Size : ", size ," \n", terminator: "");
		var i: Int = 0;
		//print upper half layers
		while (i < size)
		{
			//include initial space
			self.space((size * 2) - (i * 2));
			//print star
			self.print_star(i);
			if (i >= 2)
			{
				self.space((i - 1) * 2 - 1);
				self.print_star(i);
			}
			print("\n", terminator: "");
			i += 1;
		}
		//print bottom half layers
		i = 0;
		while (i < size)
		{
			//include initial space
			self.space(i * 2);
			//print star
			self.print_star(size - i);
			if (i + 1 != size)
			{
				self.space(((size - 1) * 2) - (i * 2) - 1);
				self.print_star(size - i);
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test evaluation
	obj.double_arrow(3);
	obj.double_arrow(4);
	obj.double_arrow(7);
	obj.double_arrow(9);
}
main();

Output

 Size :  3

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

 Size :  4

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

 Size :  7

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

 Size :  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







© 2021, kalkicode.com, All rights reserved