Print L pattern

Here given code implementation process.

//C Program 
//Print L star 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 'L' star pattern of given height
void print_l(int height)
{
	if (height <= 2)
	{
		return;
	}
	printf("\nHeight : %d  \n\n", height);
	for (int i = 0; i < height; ++i)
	{
		if (i == height - 1)
		{
			//last row of star
			print_star(height);
		}
		else
		{
			printf("*");
		}
		printf("\n");
	}
}
int main()
{
	//Simple test
	print_l(6);
	print_l(9);
	print_l(3);
	print_l(8);
	return 0;
}

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
/*
  Java Program
  Print L star 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 'L' star pattern of given height
	public void print_l(int height)
	{
		if (height <= 2)
		{
			return;
		}
		System.out.print("\nHeight : " + height + " \n\n");
		for (int i = 0; i < height; ++i)
		{
			if (i == height - 1)
			{
				//last row of star
				print_star(height);
			}
			else
			{
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_l(6);
		obj.print_l(9);
		obj.print_l(3);
		obj.print_l(8);
	}
}

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
/*
  C++ Program
  Print L star 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 'L' star pattern of given height
	void print_l(int height)
	{
		if (height <= 2)
		{
			return;
		}
		cout << "\nHeight : " << height << " \n\n";
		for (int i = 0; i < height; ++i)
		{
			if (i == height - 1)
			{
				//last row of star
				this->print_star(height);
			}
			else
			{
				cout << "*";
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj = MyPattern();
	//Simple test
	obj.print_l(6);
	obj.print_l(9);
	obj.print_l(3);
	obj.print_l(8);
	return 0;
}

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
/*
  C# Program
  Print L star 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 'L' star pattern of given height
	public void print_l(int height)
	{
		if (height <= 2)
		{
			return;
		}
		Console.Write("\nHeight : " + height + " \n\n");
		for (int i = 0; i < height; i++)
		{
			if (i == height - 1)
			{
				//last row of star
				print_star(height);
			}
			else
			{
				Console.Write("*");
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_l(6);
		obj.print_l(9);
		obj.print_l(3);
		obj.print_l(8);
	}
}

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
<?php
/*
  Php Program
  Print L star 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 'L' star pattern of given height
	function print_l($height)
	{
		if ($height <= 2)
		{
			return;
		}
		echo "\nHeight : ". $height ." \n\n";
		for ($i = 0; $i < $height; ++$i)
		{
			if ($i == $height - 1)
			{
				//last row of star
				$this->print_star($height);
			}
			else
			{
				echo "*";
			}
			echo "\n";
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->print_l(6);
	$obj->print_l(9);
	$obj->print_l(3);
	$obj->print_l(8);
}
main();

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
/*
  Node Js Program
  Print L star 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 'L' star pattern of given height
	print_l(height)
	{
		if (height <= 2)
		{
			return;
		}
		process.stdout.write("\nHeight : " + height + " \n\n");
		for (var i = 0; i < height; ++i)
		{
			if (i == height - 1)
			{
				//last row of star
				this.print_star(height);
			}
			else
			{
				process.stdout.write("*");
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var obj = new MyPattern();
	//Simple test
	obj.print_l(6);
	obj.print_l(9);
	obj.print_l(3);
	obj.print_l(8);
}
main();

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
#   Python 3 Program
#   Print L star 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
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	# Print 'L' star pattern of given height
	def print_l(self, height) :
		if (height <= 2) :
			return
		
		print("\nHeight : ", height ," \n\n", end = "")
		i = 0
		while (i < height) :
			if (i == height - 1) :
				# last row of star
				self.print_star(height)
			else :
				print("*", end = "")
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.print_l(6)
	obj.print_l(9)
	obj.print_l(3)
	obj.print_l(8)

if __name__ == "__main__": main()

Output

Height :  6

*
*
*
*
*
* * *

Height :  9

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

Height :  3

*
*
* *

Height :  8

*
*
*
*
*
*
*
* * * *
#   Ruby Program
#   Print L star 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
		while (counter < size)
		
			# Add space
			print(" ")
			counter += 1
		end
	end
	# Print 'L' star pattern of given height
	def print_l(height)
	
		if (height <= 2)
		
			return
		end
		print("\nHeight : ", height ," \n\n")
		i = 0
		while (i < height)
		
			if (i == height - 1)
			
				# last row of star
				self.print_star(height)
			else
			
				print("*")
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.print_l(6)
	obj.print_l(9)
	obj.print_l(3)
	obj.print_l(8)
end
main()

Output

Height : 6 

*
*
*
*
*
* * * 

Height : 9 

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

Height : 3 

*
*
* *

Height : 8 

*
*
*
*
*
*
*
* * * * 
/*
  Scala Program
  Print L star 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;
		while (counter < size)
		{
			//Add space
			print(" ");
			counter += 1;
		}
	}
	//Print 'L' star pattern of given height
	def print_l(height: Int): Unit = {
		if (height <= 2)
		{
			return;
		}
		print("\nHeight : " + height + " \n\n");
		var i: Int = 0;
		while (i < height)
		{
			if (i == height - 1)
			{
				//last row of star
				print_star(height);
			}
			else
			{
				print("*");
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.print_l(6);
		obj.print_l(9);
		obj.print_l(3);
		obj.print_l(8);
	}
}

Output

Height : 6

*
*
*
*
*
* * *

Height : 9

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

Height : 3

*
*
* *

Height : 8

*
*
*
*
*
*
*
* * * *
/*
  Swift Program
  Print L star 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;
		while (counter < size)
		{
			print(" ", terminator: "");
			counter += 1;
		}
	}
	//Print "L" star pattern of given height
	func print_l(_ height: Int)
	{
		if (height <= 2)
		{
			return;
		}
		print("\nHeight : ", height ," \n\n", terminator: "");
		var i: Int = 0;
		while (i < height)
		{
			if (i == height - 1)
			{
				//last row of star
				self.print_star(height);
			}
			else
			{
				print("*", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.print_l(6);
	obj.print_l(9);
	obj.print_l(3);
	obj.print_l(8);
}
main();

Output

Height :  6

*
*
*
*
*
* * *

Height :  9

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

Height :  3

*
*
* *

Height :  8

*
*
*
*
*
*
*
* * * *


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