Skip to main content

Print L shape of given size

Here given code implementation process.

//C Program 
//Print L shape of given size
#include <stdio.h>

//Print L shape star pattern of given size
void print_l_shape(int size)
{
	if (size <= 2 || size % 2 != 0)
	{
		return;
	}
	printf("\nsize : %d  \n\n", size);
	//Loop controlling variables
	int i = 0, j = 0;
	//Loop, which is control the printing row operations
	for (i = 0; i < size; ++i)
	{
		//Loop, which is control the printing column operations
		for (j = 0; j < size; ++j)
		{
			//Condition which is printing a star element
			if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
			{
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
int main()
{
	//Simple test
	print_l_shape(6);
	print_l_shape(14);
	print_l_shape(10);
	return 0;
}

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
/*
Java program
Print L shape of given size
*/
class MyPattern
{
	//Print L shape star pattern of given size
	public void print_l_shape(int size)
	{
		if (size <= 2 || size % 2 != 0)
		{
			return;
		}
		System.out.print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
				{
					System.out.print("* ");
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_l_shape(6);
		obj.print_l_shape(14);
		obj.print_l_shape(10);
	}
}

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
/*
C++ program
Print L shape of given size
*/
//Include header file
#include <iostream>

using namespace std;
class MyPattern
{
	public:
		//Print L shape star pattern of given size
		void print_l_shape(int size)
		{
			if (size <= 2 || size % 2 != 0)
			{
				return;
			}
			cout << "\nsize : " << size << " \n\n";
			//Loop controlling variables
			int i = 0, j = 0;
			//Loop, which is control the printing row operations
			for (i = 0; i < size; ++i)
			{
				//Loop, which is control the printing column operations
				for (j = 0; j < size; ++j)
				{
					//Condition which is printing a star element
					if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
					{
						cout << "* ";
					}
					else
					{
						cout << " ";
					}
				}
				cout << "\n";
			}
		}
};
int main()
{
	MyPattern obj = MyPattern();
	//Simple test
	obj.print_l_shape(6);
	obj.print_l_shape(14);
	obj.print_l_shape(10);
	return 0;
}

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
/*
C# program
Print L shape of given size
*/
//Include namespace system
using System;
class MyPattern
{
	//Print L shape star pattern of given size
	public void print_l_shape(int size)
	{
		if (size <= 2 || size % 2 != 0)
		{
			return;
		}
		Console.Write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
				{
					Console.Write("* ");
				}
				else
				{
					Console.Write(" ");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_l_shape(6);
		obj.print_l_shape(14);
		obj.print_l_shape(10);
	}
}

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
<?php
/*
Php program
Print L shape of given size
*/
class MyPattern
{
	//Print L shape star pattern of given size
	public	function print_l_shape($size)
	{
		if ($size <= 2 || $size % 2 != 0)
		{
			return;
		}
		echo "\nsize : ". $size ." \n\n";
		//Loop controlling variables
		$i = 0;
		$j = 0;
		//Loop, which is control the printing row operations
		for ($i = 0; $i < $size; ++$i)
		{
			//Loop, which is control the printing column operations
			for ($j = 0; $j < $size; ++$j)
			{
				//Condition which is printing a star element
				if ($j == 0 || ($j < intval($size / 2)) || $i == $size - 1 || ($i >= intval($size / 2) && $j >= intval($size / 2)))
				{
					echo "* ";
				}
				else
				{
					echo " ";
				}
			}
			echo "\n";
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->print_l_shape(6);
	$obj->print_l_shape(14);
	$obj->print_l_shape(10);
}
main();

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
/*
Node Js program
Print L shape of given size
*/
class MyPattern
{
	//Print L shape star pattern of given size
	print_l_shape(size)
	{
		if (size <= 2 || size % 2 != 0)
		{
			return;
		}
		process.stdout.write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i = 0;
		var j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (j == 0 || (j < parseInt(size / 2)) || i == size - 1 || (i >= parseInt(size / 2) && j >= parseInt(size / 2)))
				{
					process.stdout.write("* ");
				}
				else
				{
					process.stdout.write(" ");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var obj = new MyPattern();
	//Simple test
	obj.print_l_shape(6);
	obj.print_l_shape(14);
	obj.print_l_shape(10);
}
main();

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
# Python 3 program
# Print L shape of given size

class MyPattern :
	# Print L shape star pattern of given size
	def print_l_shape(self, size) :
		if (size <= 2 or size % 2 != 0) :
			return
		
		print("\nsize : ", size ," \n\n", end = "")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size) :
			j = 0
			# Loop, which is control the printing column operations
			while (j < size) :
				# Condition which is printing a star element
				if (j == 0 or(j < int(size / 2)) or i == size - 1 or(i >= int(size / 2) and j >= int(size / 2))) :
					print("* ", end = "")
				else :
					print(" ", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.print_l_shape(6)
	obj.print_l_shape(14)
	obj.print_l_shape(10)

if __name__ == "__main__": main()

Output

size :  6

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

size :  14

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

size :  10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
# Ruby program
# Print L shape of given size

class MyPattern

	# Print L shape star pattern of given size
	def print_l_shape(size)
	
		if (size <= 2 || size % 2 != 0)
		
			return
		end
		print("\nsize : ", size ," \n\n")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size)
		
			j = 0
			# Loop, which is control the printing column operations
			while (j < size)
			
				# Condition which is printing a star element
				if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
				
					print("* ")
				else
				
					print(" ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.print_l_shape(6)
	obj.print_l_shape(14)
	obj.print_l_shape(10)
end
main()

Output

size : 6 

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

size : 14 

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

size : 10 

* * * * *      
* * * * *      
* * * * *      
* * * * *      
* * * * *      
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
/*
Scala program
Print L shape of given size
*/
class MyPattern
{
	//Print L shape star pattern of given size
	def print_l_shape(size: Int): Unit = {
		if (size <= 2 || size % 2 != 0)
		{
			return;
		}
		print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (j == 0 || (j < (size / 2).toInt) || i == size - 1 || (i >= (size / 2).toInt && j >= (size / 2).toInt))
				{
					print("* ");
				}
				else
				{
					print(" ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.print_l_shape(6);
		obj.print_l_shape(14);
		obj.print_l_shape(10);
	}
}

Output

size : 6

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

size : 14

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

size : 10

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
/*
Swift program
Print L shape of given size
*/
class MyPattern
{
	//Print L shape star pattern of given size
	func print_l_shape(_ size: Int)
	{
		if (size <= 2 || size % 2 != 0)
		{
			return;
		}
		print("\nsize : ", size ," \n\n", terminator: "");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (j == 0 || (j < size / 2) || i == size - 1 || (i >= size / 2 && j >= size / 2))
				{
					print("* ", terminator: "");
				}
				else
				{
					print(" ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.print_l_shape(6);
	obj.print_l_shape(14);
	obj.print_l_shape(10);
}
main();

Output

size :  6

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

size :  14

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

size :  10

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




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