Print L shape of given size
In this article, we will discuss a C program that prints an L shape of a given size using stars ('*') and spaces. The L shape consists of horizontal and vertical lines, forming the letter "L" on the screen. We will go through the problem statement, provide an explanation with a suitable example, present the algorithm and pseudocode, analyze the time complexity of the code, and explain the resultant output.
Problem Statement
The problem is to write a program that takes an integer size as input and prints an L shape using stars and spaces. The size represents the length and width of the L shape. The program should satisfy the following conditions:
- The size must be greater than 2 and even because the L shape needs a minimum size of 3x3 to be recognizable.
- The L shape should be formed using stars ('*') and spaces. Stars represent the outline of the L, while spaces fill the inner part of the L shape.
- The L shape should be symmetric and aligned to the top-left corner of the output.
Explanation with Example
Let's understand the problem with an example. Suppose we have a size of 6. The output for this input will be:
* * * * * * * * * * * * * * * * * * * * * * * * * * *
As we can see, the L shape is formed using stars and spaces. The stars create the outline of the L, while the spaces fill the inner part.
Algorithm and Pseudocode
To solve this problem, we can use nested loops to iterate over each position in the output and print either a star or a space
based on the conditions defined. Here is the algorithm and pseudocode for the print_l_shape
function:
function printLShape(size):
if size <= 2 or size is odd:
return
for row from 0 to size-1:
for col from 0 to size-1:
if col is 0 or (col < size/2) or row is size-1 or (row >= size/2 and col >= size/2):
print "* "
else:
print " "
print a new line
// Example usage:
printLShape(6)
printLShape(14)
printLShape(10)
Explanation of Algorithm
The algorithm starts by checking if the input size is less than or equal to 2 or odd. In such cases, the L shape cannot be formed, so the function returns. Then, two nested loops are used to iterate over each position in the output matrix. The outer loop controls the row operations, while the inner loop controls the column operations. For each position, the algorithm checks the conditions to determine whether to print a star or a space. The conditions ensure that the stars form the outline of the L shape, while the spaces fill the inner part. Finally, a newline character is printed to move to the next row.
Code Solution
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
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
Time Complexity of the Code
The time complexity of the code is O(size^2) because it uses two nested loops that iterate over the size of the input. As the input size increases, the number of iterations grows quadratically.
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