Skip to main content

Print Y pattern

Here given code implementation process.

// C Program
// Print Y pattern 
#include <stdio.h>

//include space 
void space(int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf(" ");
	}
}
//Display upper layer of y pattern
void show_v(int size)
{
	if (size < 2)
	{
		return;
	}
	int side = (size *2) - 3;
	/*
	In given below loop is display v pattern. for example size 3
	    
	    *  *
	     **
	      *
	*/
	for (int i = 0; i < size; i++)
	{
		space(i);
		printf("*");
		space(side - i);
		if (i < side)
		{
			printf("*");
		}
		side--;
		printf("\n");
	}
}
//This method are handle the request to print Y pattern
void show_y(int size)
{
	if (size < 3 || size % 2 == 0)
	{
		//Some invalid pattern size
		return;
	}
	//Assuming the size odd number is greater than 3
	printf("Size : %d\n\n", size);
	//Calculate upper V pattern size
	int i = (size / 2) + (size / 4) + 1;
	show_v(i);
	//Get remaining bottom rows
	int j = size - i;
	//This loop are print the bottom layer of y pattern
	while (j > 0)
	{
		space(i - 1);
		j--;
		printf("*\n");
	}
	printf("\n");
}
int main()
{
	//Test evaluation
	show_y(5);
	show_y(7);
	show_y(11);
	show_y(3);
	return 0;
}

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
/*
  Java Program
  Print Y pattern 
*/
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" ");
		}
	}
	//Display upper layer of y pattern
	public void show_v(int size)
	{
		if (size < 2)
		{
			return;
		}
		int side = (size * 2) - 3;
		for (int i = 0; i < size; i++)
		{
			space(i);
			System.out.print("*");
			space(side - i);
			if (i < side)
			{
				System.out.print("*");
			}
			side--;
			System.out.print("\n");
		}
	}
	//This method are handle the request to print Y pattern
	public void show_y(int size)
	{
		if (size < 3 || size % 2 == 0)
		{
			//Some invalid pattern size
			return;
		}
		//Assuming the size odd number is greater than 3
		System.out.print("Size : " + size + "\n\n");
		//Calculate upper V pattern size
		int i = (size / 2) + (size / 4) + 1;
		show_v(i);
		//Get remaining bottom rows
		int j = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			space(i - 1);
			j--;
			System.out.print("*\n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test evaluation
		obj.show_y(5);
		obj.show_y(7);
		obj.show_y(11);
		obj.show_y(3);
	}
}

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
/*
  C++ Program
  Print Y pattern 
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public:
    //include space 
    void space(int size)
    {
      for (int i = 0; i < size; ++i)
      {
        cout << " ";
      }
    }
	//Display upper layer of y pattern
	void show_v(int size)
	{
		if (size < 2)
		{
			return;
		}
		int side = (size * 2) - 3;
		for (int i = 0; i < size; i++)
		{
			this->space(i);
			cout << "*";
			this->space(side - i);
			if (i < side)
			{
				cout << "*";
			}
			side--;
			cout << "\n";
		}
	}
	//This method are handle the request to print Y pattern
	void show_y(int size)
	{
		if (size < 3 || size % 2 == 0)
		{
			//Some invalid pattern size
			return;
		}
		cout << "Size : " << size << "\n\n";
		//Calculate upper V pattern size
		int i = (size / 2) + (size / 4) + 1;
		this->show_v(i);
		//Get remaining bottom rows
		int j = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			this->space(i - 1);
			j--;
			cout << "*\n";
		}
		cout << "\n";
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Test evaluation
	obj.show_y(5);
	obj.show_y(7);
	obj.show_y(11);
	obj.show_y(3);
	return 0;
}

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
/*
  C# Program
  Print Y Star Pattern 
*/
using System;
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write(" ");
		}
	}
	//Display upper layer of y pattern
	public void show_v(int size)
	{
		if (size < 2)
		{
			return;
		}
		int side = (size * 2) - 3;
		for (int i = 0; i < size; i++)
		{
			space(i);
			Console.Write("*");
			space(side - i);
			if (i < side)
			{
				Console.Write("*");
			}
			side--;
			Console.Write("\n");
		}
	}
	//This method are handle the request to print Y pattern
	public void show_y(int size)
	{
		//Some invalid pattern size
		if (size < 3 || size % 2 == 0)
		{
			return;
		}
		//Assuming the size odd number is greater than 3
		Console.Write("Size : " + size + "\n\n");
		//Calculate upper V pattern size
		int i = (size / 2) + (size / 4) + 1;
		show_v(i);
		//Get remaining bottom rows
		int j = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			space(i - 1);
			j--;
			Console.Write("*\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test evaluation
		obj.show_y(5);
		obj.show_y(7);
		obj.show_y(11);
		obj.show_y(3);
	}
}

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
<?php
/*
  Php Program
  Print Y Star Pattern 
*/
class MyPattern
{
	//include space 
	public function space($size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo(" ");
		}
	}
	//Display upper layer of y pattern
	public function show_v($size)
	{
		if ($size < 2)
		{
			return;
		}
		$side = ($size * 2) - 3;
		for ($i = 0; $i < $size; $i++)
		{
			$this->space($i);
			echo("*");
			$this->space($side - $i);
			if ($i < $side)
			{
				echo("*");
			}
			$side--;
			echo("\n");
		}
	}
	//This method are handle the request to print Y pattern
	public function show_y($size)
	{
		if ($size < 3 || $size % 2 == 0)
		{
			return;
		}
		//Assuming the size odd number is greater than 3
		echo("Size : ". $size ."\n\n");
		//Calculate upper V pattern size
		$i = (intval($size / 2)) + (intval($size / 4)) + 1;
		$this->show_v($i);
		//Get remaining bottom rows
		$j = $size - $i;
		//This loop are print the bottom layer of y pattern
		while ($j > 0)
		{
			$this->space($i - 1);
			$j--;
			echo("*\n");
		}
		echo("\n");
	}
}

function main()
{
	$obj = new MyPattern();
	//Test evaluation
	$obj->show_y(5);
	$obj->show_y(7);
	$obj->show_y(11);
	$obj->show_y(3);
}
main();

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
/*
  Node Js Program
  Print Y Star Pattern 
*/
class MyPattern
{
	//include space 
	space(size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" ");
		}
	}
	//Display upper layer of y pattern
	show_v(size)
	{
		if (size < 2)
		{
			return;
		}
		var side = (size * 2) - 3;
		for (var i = 0; i < size; i++)
		{
			this.space(i);
			process.stdout.write("*");
			this.space(side - i);
			if (i < side)
			{
				process.stdout.write("*");
			}
			side--;
			process.stdout.write("\n");
		}
	}
	//This method are handle the request to print Y pattern
	show_y(size)
	{
		if (size < 3 || size % 2 == 0)
		{
			return;
		}
		//Assuming the size odd number is greater than 3
		process.stdout.write("Size : " + size + "\n\n");
		//Calculate upper V pattern size
		var i = (parseInt(size / 2)) + (parseInt(size / 4)) + 1;
		this.show_v(i);
		//Get remaining bottom rows
		var j = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			this.space(i - 1);
			j--;
			process.stdout.write("*\n");
		}
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test evaluation
	obj.show_y(5);
	obj.show_y(7);
	obj.show_y(11);
	obj.show_y(3);
}
main();

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
#   Python 3 Program
#   Print Y Star Pattern 

class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(end = " ")
			i += 1
		
	
	# Display upper layer of y pattern
	def show_v(self, size) :
		if (size < 2) :
			return
		
		side = (size * 2) - 3
		i = 0
		while (i < size) :
			self.space(i)
			print(end = "*")
			self.space(side - i)
			if (i < side) :
				print(end = "*")
			
			side -= 1
			print(end = "\n")
			i += 1
		
	
	# This method are handle the request to print Y pattern
	def show_y(self, size) :
		# Some invalid pattern size
		if (size < 3 or size % 2 == 0) :
			return
		
		print("Size : ", size ,"\n")
		# Calculate upper V pattern size
		i = (int(size / 2)) + (int(size / 4)) + 1
		self.show_v(i)
		# Get remaining bottom rows
		j = size - i
		# This loop are print the bottom layer of y pattern
		while (j > 0) :
			self.space(i - 1)
			j -= 1
			print("*")
		
		print(end = "\n")
	

def main() :
	obj = MyPattern()
	# Test evaluation
	obj.show_y(5)
	obj.show_y(7)
	obj.show_y(11)
	obj.show_y(3)


if __name__ == "__main__": main()

Output

Size :  5

*     *
 *   *
  * *
   *
   *

Size :  7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size :  11

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

Size :  3

* *
 *
 *
/*
  Scala Program
  Print Y Star Pattern 
*/
class MyPattern
{
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" ");
			i += 1;
		}
	}
	//Display upper layer of y pattern
	def show_v(size: Int): Unit = {
		if (size < 2)
		{
			return;
		}
		var side: Int = (size * 2) - 3;
		var i: Int = 0;
		while (i < size)
		{
			space(i);
			print("*");
			space(side - i);
			if (i < side)
			{
				print("*");
			}
			side -= 1;
			print("\n");
			i += 1;
		}
	}
	//This method are handle the request to print Y pattern
	def show_y(size: Int): Unit = {
		if (size < 3 || size % 2 == 0)
		{
			return;
		}
		print("Size : " + size + "\n\n");
		//Calculate upper V pattern size
		var i: Int = ((size / 2).toInt) + ((size / 4).toInt) + 1;
		show_v(i);
		//Get remaining bottom rows
		var j: Int = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			space(i - 1);
			j -= 1;
			print("*\n");
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test evaluation
		obj.show_y(5);
		obj.show_y(7);
		obj.show_y(11);
		obj.show_y(3);
	}
}

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *
/*
  Swift Program
  Print Y Star Pattern 
*/
class MyPattern
{
	//include space 
	func space(_ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	//Display upper layer of y pattern
	func show_v(_ size: Int)
	{
		if (size < 2)
		{
			return;
		}
		var side: Int = (size * 2) - 3;
		var i: Int = 0;
		while (i < size)
		{
			self.space(i);
			print("*", terminator: "");
			self.space(side - i);
			if (i < side)
			{
				print("*", terminator: "");
			}
			side -= 1;
			print("\n", terminator: "");
			i += 1;
		}
	}
	//This method are handle the request to print Y pattern
	func show_y(_ size: Int)
	{
		if (size < 3 || size % 2 == 0)
		{
			return;
		}
		print("Size : ", size ,"\n\n", terminator: "");
		//Calculate upper V pattern size
		let i: Int = (size / 2) + (size / 4) + 1;
		self.show_v(i);
		//Get remaining bottom rows
		var j: Int = size - i;
		//This loop are print the bottom layer of y pattern
		while (j > 0)
		{
			self.space(i - 1);
			j -= 1;
			print("*\n", terminator: "");
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test evaluation
	obj.show_y(5);
	obj.show_y(7);
	obj.show_y(11);
	obj.show_y(3);
}
main();

Output

Size :  5

*     *
 *   *
  * *
   *
   *

Size :  7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size :  11

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

Size :  3

* *
 *
 *
#   Ruby Program
#   Print Y Star Pattern 

class MyPattern

	# include space 
	def space(size)
	
		i = 0
		while (i < size)
		
			print(" ")
			i += 1
		end
	end
	# Display upper layer of y pattern
	def show_v(size)
	
		if (size < 2)
		
			return
		end
		side = (size * 2) - 3
		i = 0
		while (i < size)
		
			self.space(i)
			print("*")
			self.space(side - i)
			if (i < side)
			
				print("*")
			end
			side -= 1
			print("\n")
			i += 1
		end
	end
	# This method are handle the request to print Y pattern
	def show_y(size)
	
		# Some invalid pattern size
		if (size < 3 || size % 2 == 0)
		
			return
		end
		print("Size : ", size ,"\n\n")
		# Calculate upper V pattern size
		i = (size / 2) + (size / 4) + 1
		self.show_v(i)
		# Get remaining bottom rows
		j = size - i
		# This loop are print the bottom layer of y pattern
		while (j > 0)
		
			self.space(i - 1)
			j -= 1
			print("*\n")
		end
		print("\n")
	end
end
def main()

	obj = MyPattern.new()
	# Test evaluation
	obj.show_y(5)
	obj.show_y(7)
	obj.show_y(11)
	obj.show_y(3)
end
main()

Output

Size : 5

*     *
 *   *
  * *
   *
   *

Size : 7

*       *
 *     *
  *   *
   * *
    *
    *
    *

Size : 11

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

Size : 3

* *
 *
 *





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