Print Y pattern
In this article, we will discuss how to print the Y pattern using a simple C program. The Y pattern consists of a series of asterisks (*) arranged in the shape of the letter "Y". We will explore the problem statement, provide an explanation with suitable examples, present the algorithm and pseudocode, and explain the resultant output with time complexity analysis.
Problem Statement
The task is to write a C program that prints the Y pattern based on the given size. The size determines the height and width of the pattern. The pattern consists of two parts: an upper layer in the shape of the letter "V" and a bottom layer that completes the "Y" shape. The program should handle various input sizes and display the corresponding Y pattern.
Example
Let's consider an example to better understand the Y pattern. For a size of 5, the pattern would look like this:
* *
* *
* *
*
*
As we can see, the upper layer forms a "V" shape, and the bottom layer completes the "Y" shape. The spaces are used to align the asterisks correctly.
Algorithm and Pseudocode
Here is the algorithm to print the Y pattern:
- Define a function to print spaces. This function will be used to align the asterisks.
- Define a function to display the upper layer of the pattern.
- Inside the upper layer function:
- Check if the size is less than 2. If so, return.
- Calculate the side length for the V pattern.
- Use a loop to print the V pattern, iterating from 0 to size:
- Call the space function to print the required spaces.
- Print an asterisk (*)
- Call the space function again to print the remaining spaces.
- If the current iteration is less than the side length, print an asterisk (*) to form the V shape.
- Decrement the side length.
- Print a new line character (\n).
- Define a function to display the complete Y pattern.
- Inside the Y pattern function:
- Check if the size is less than 3 or an even number. If so, return.
- Calculate the size for the upper layer of the pattern.
- Call the upper layer function to display the upper layer.
- Calculate the number of remaining bottom rows.
- Use a loop to print the bottom layer of the pattern, iterating while the remaining row count is greater than 0:
- Call the space function to print the required spaces.
- Decrement the remaining row count.
- Print an asterisk (*)
- Print a new line character (\n).
- In the main function:
- Call the Y pattern function with different sizes to test and display the patterns.
- Return 0 to indicate successful execution.
Here is the pseudocode for the Y pattern program:
space(size):
for i from 0 to size - 1:
print " "
show_v(size):
if size < 2:
return
side = (size * 2) - 3
for i from 0 to size - 1:
space(i)
print "*"
space(side - i)
if i < side:
print "*"
side--
print "\n"
show_y(size):
if size < 3 or size % 2 == 0:
return
upper_layer_size = (size / 2) + (size / 4) + 1
show_v(upper_layer_size)
remaining_rows = size - upper_layer_size
while remaining_rows > 0:
space(upper_layer_size - 1)
remaining_rows--
print "*\n"
main():
show_y(5)
show_y(7)
show_y(11)
show_y(3)
return 0
Code Solution
// 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
* *
*
*
The output shows the Y pattern for different input sizes. Each asterisk (*) represents a position in the pattern, and the spaces are used for alignment. The upper layer forms a "V" shape, and the bottom layer completes the "Y" shape. The patterns are printed according to the specified sizes, maintaining the correct structure.
Time Complexity
The time complexity of the Y pattern program is O(n^2), where n is the input size. This complexity arises from the nested loops used to iterate through the pattern positions and print the required characters. The program performs linear operations based on the size to calculate the pattern and print it accordingly.
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