# Print L pattern

In this article, we will discuss the problem of printing an L pattern using stars (*) in the C programming language. We will provide a detailed explanation of the problem, along with a suitable example and the algorithm to solve it. Additionally, we will include pseudocode and analyze the time complexity of the code.

### Problem Statement

The task is to write a program that prints the L pattern using stars. The pattern should be printed based on the given height. The L pattern consists of a vertical line of stars followed by a horizontal line of stars at the bottom. The spaces are filled with empty spaces. For example, if the height is 6, the pattern would be printed as follows:

*
*
*
*
*
* * *

### Explanation with Example

Let's take the example of a height of 6 to understand the pattern.

To print the pattern, we iterate through each row from 0 to height-1. For each row, if it is the last row (height-1), we print a horizontal line of stars. Otherwise, we print a single star and move to the next line. This process continues until we have printed the required number of rows. In the given example, when the height is 6, the first five rows are printed with a single star in each row. Finally, the last row is printed with a horizontal line of stars. This creates the L pattern.

### Algorithm

Here is the algorithm to solve the problem:

1. Define a function named print_star that takes an integer size as a parameter.
2. Initialize a counter variable to 0.
3. Iterate through the loop until the counter is less than size:
• If the counter is even, print a star.
• Otherwise, print a space.
4. Define a function named print_space that takes an integer size as a parameter.
5. Initialize a counter variable to 0.
6. Iterate through the loop until the counter is less than size:
• Print a space.
7. Define a function named print_l that takes an integer height as a parameter.
8. If height is less than or equal to 2, return from the function.
9. Print the height value and a newline character to indicate the start of the pattern.
10. Iterate through the loop from 0 to height-1:
• If the current row is the last row (height-1), call the print_star function with the height parameter.
• Otherwise, print a single star.
• Print a newline character to move to the next line.
11. In the main function:
• Call the print_l function with different height values to test the code.

### Pseudocode

Here is the pseudocode for the given problem:

function print_star(size):
counter = 0
while counter < size:
if counter is even:
print "*"
else:
print " "
increment counter

function print_space(size):
counter = 0
while counter < size:
print " "
increment counter

function print_l(height):
if height <= 2:
return
print "Height: ", height
for i from 0 to height-1:
if i is height-1:
call print_star function with height parameter
else:
print "*"
print newline

function main():
call print_l function with different height values

call main function

## Code Solution

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)
{
printf("*");
}
else
{
printf(" ");
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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)

print("*")
else

print(" ")
end
counter += 1
end
end
# Display space of given size
def print_space(size)

counter = 0
while (counter < size)

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)
{
print("*");
}
else
{
print(" ");
}
counter += 1;
}
}
//Display space of given size
def print_space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size)
{
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();

Height :  6

*
*
*
*
*
* * *

Height :  9

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

Height :  3

*
*
* *

Height :  8

*
*
*
*
*
*
*
* * * *

### Time Complexity

The time complexity of the code depends on the height parameter. The print_l function iterates through each row from 0 to height-1. Within each iteration, the print_star function is called once. Hence, the time complexity can be approximated to O(height).

## 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.