Posted on by Kalkicode
Code Pattern

Print chevron shape of given size

In this article, we will discuss how to print a chevron shape of a given size using a simple algorithm. The chevron shape consists of a series of lines that resemble an inverted "V" pattern. We will provide a problem statement, explain the algorithm and pseudocode, and analyze the time complexity of the code. Let's dive in!

Problem Statement

We want to write a program that takes an integer as input, representing the size of the chevron shape, and prints the chevron pattern using asterisks (*) and spaces. The chevron shape will have a height equal to the given size.

For example, if the input size is 7, the output should be:

```  * * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
```

Similarly, for a size of 9, the output should be:

```  * * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
```

Algorithm and Pseudocode

1. Start by defining the function `include_space` that prints a given number of spaces.

2. Implement the function `print_chevron_shape` that takes the size as input and follows the following steps:

1. Check if the size is less than or equal to 2 or if it is an even number. If either condition is true, return from the function.
2. Print the height of the chevron pattern as a visual aid.
3. Initialize loop variables `i` and `j`.
4. Iterate over the rows from 0 to size-1:
• If `i` is less than half of the size, call the `include_space` function with `i + 1` to print the appropriate number of spaces.
• Otherwise, call the `include_space` function with `size - i` to print the appropriate number of spaces.
• Iterate over the columns from 0 to size/2:
• Print an asterisk followed by a space.
• Print a new line to move to the next row.

The pseudocode representation of the algorithm is as follows:

``````function include_space(size):
for i from 0 to size * 2:
print " "

function print_chevron_shape(size):
if size <= 2 or size is even:
return
print "Height: size"
for i from 0 to size-1:
if i < size / 2:
include_space(i + 1)
else:
include_space(size - i)
for j from 0 to size / 2:
print "* "
print new line``````

Code Solution

Here given code implementation process.

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

//include space of given size
void include_space(int size)
{
//include double space in given size
for (int i = 0; i < size * 2; ++i)
{
printf(" ");
}
}
//Print chevron shape star pattern of given size
void print_chevron_shape(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
printf("\n Height : %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)
{
if (i < size / 2)
{
include_space(i + 1);
}
else
{
include_space(size - i);
}
//loop, which is control the printing column operations
for (j = 0; j <= size / 2; ++j)
{
printf("* ");
}
printf("\n");
}
}
int main()
{
//Simple test
print_chevron_shape(7);
print_chevron_shape(9);
print_chevron_shape(11);
return 0;
}``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````/*
Java program
Print chevron shape of given size
*/
class MyPattern
{
//include space of given size
public void include_space(int size)
{
//include double space in given size
for (int i = 0; i < size * 2; ++i)
{
System.out.print(" ");
}
}
//Print chevron shape star pattern of given size
public void print_chevron_shape(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
System.out.print("\n Height : " + 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)
{
if (i < size / 2)
{
include_space(i + 1);
}
else
{
include_space(size - i);
}
//loop, which is control the printing column operations
for (j = 0; j <= size / 2; ++j)
{
System.out.print("* ");
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
}
}``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````/*
C++ program
Print chevron shape of given size
*/
#include <iostream>

using namespace std;
class MyPattern
{
public:
//include space of given size
void include_space(int size)
{
//include double space in given size
for (int i = 0; i < size * 2; ++i)
{
cout << " ";
}
}
//Print chevron shape star pattern of given size
void print_chevron_shape(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
cout << "\n Height : " << 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)
{
if (i < size / 2)
{
include_space(i + 1);
}
else
{
include_space(size - i);
}
//loop, which is control the printing column operations
for (j = 0; j <= size / 2; ++j)
{
cout << "* ";
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
return 0;
}``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````/*
C# program
Print chevron shape of given size
*/
//Include namespace system
using System;
class MyPattern
{
//include space of given size
public void include_space(int size)
{
//include double space in given size
for (int i = 0; i < size * 2; ++i)
{
Console.Write(" ");
}
}
//Print chevron shape star pattern of given size
public void print_chevron_shape(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
Console.Write("\n Height : " + 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)
{
if (i < size / 2)
{
include_space(i + 1);
}
else
{
include_space(size - i);
}
//loop, which is control the printing column operations
for (j = 0; j <= size / 2; ++j)
{
Console.Write("* ");
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
}
}``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````<?php
/*
Php program
Print chevron shape of given size
*/
class MyPattern
{
//include space of given size
public	function include_space(\$size)
{
//include double space in given size
for (\$i = 0; \$i < \$size * 2; ++\$i)
{
echo " ";
}
}
//Print chevron shape star pattern of given size
public	function print_chevron_shape(\$size)
{
if (\$size <= 2 || \$size % 2 == 0)
{
return;
}
echo "\n Height : ". \$size ." \n\n";
//loop controlling variables
\$i = 0;
\$j = 0;
//loop, which is control the printing row operations
for (\$i = 0; \$i < \$size; ++\$i)
{
if (\$i < intval(\$size / 2))
{
\$this->include_space(\$i + 1);
}
else
{
\$this->include_space(\$size - \$i);
}
//loop, which is control the printing column operations
for (\$j = 0; \$j <= intval(\$size / 2); ++\$j)
{
echo "* ";
}
echo "\n";
}
}
}

function main()
{
\$obj = new MyPattern();
//Simple test
\$obj->print_chevron_shape(7);
\$obj->print_chevron_shape(9);
\$obj->print_chevron_shape(11);
}
main();``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````/*
Node Js program
Print chevron shape of given size
*/
class MyPattern
{
//include space of given size
include_space(size)
{
//include double space in given size
for (var i = 0; i < size * 2; ++i)
{
process.stdout.write(" ");
}
}
//Print chevron shape star pattern of given size
print_chevron_shape(size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
process.stdout.write("\n Height : " + 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)
{
if (i < parseInt(size / 2))
{
this.include_space(i + 1);
}
else
{
this.include_space(size - i);
}
//loop, which is control the printing column operations
for (j = 0; j <= parseInt(size / 2); ++j)
{
process.stdout.write("* ");
}
process.stdout.write("\n");
}
}
}

function main()
{
var obj = new MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
}
main();``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````# Python 3 program
# Print chevron shape of given size

class MyPattern :
# include space of given size
def include_space(self, size) :
# include double space in given size
i = 0
while (i < size * 2) :
print(" ", end = "")
i += 1

# Print chevron shape star pattern of given size
def print_chevron_shape(self, size) :
if (size <= 2 or size % 2 == 0) :
return

print("\n Height : ", size ," \n\n", end = "")
# loop controlling variables
i = 0
j = 0
# loop, which is control the printing row operations
while (i < size) :
if (i < int(size / 2)) :
self.include_space(i + 1)
else :
self.include_space(size - i)

j = 0
# loop, which is control the printing column operations
while (j <= int(size / 2)) :
print("* ", end = "")
j += 1

print("\n", end = "")
i += 1

def main() :
obj = MyPattern()
# Simple test
obj.print_chevron_shape(7)
obj.print_chevron_shape(9)
obj.print_chevron_shape(11)

if __name__ == "__main__": main()``````

Output

`````` Height :  7

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

Height :  9

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

Height :  11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````# Ruby program
# Print chevron shape of given size

class MyPattern

# include space of given size
def include_space(size)

# include double space in given size
i = 0
while (i < size * 2)

print(" ")
i += 1
end
end
# Print chevron shape star pattern of given size
def print_chevron_shape(size)

if (size <= 2 || size % 2 == 0)

return
end
print("\n Height : ", size ," \n\n")
# loop controlling variables
i = 0
j = 0
# loop, which is control the printing row operations
while (i < size)

if (i < size / 2)

self.include_space(i + 1)
else

self.include_space(size - i)
end
j = 0
# loop, which is control the printing column operations
while (j <= size / 2)

print("* ")
j += 1
end
print("\n")
i += 1
end
end
end
def main()

obj = MyPattern.new()
# Simple test
obj.print_chevron_shape(7)
obj.print_chevron_shape(9)
obj.print_chevron_shape(11)
end
main()``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
``````
``````/*
Scala program
Print chevron shape of given size
*/
class MyPattern
{
//include space of given size
def include_space(size: Int): Unit = {
//include double space in given size
var i: Int = 0;
while (i < size * 2)
{
print(" ");
i += 1;
}
}
//Print chevron shape star pattern of given size
def print_chevron_shape(size: Int): Unit = {
if (size <= 2 || size % 2 == 0)
{
return;
}
print("\n Height : " + 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)
{
if (i < (size / 2).toInt)
{
include_space(i + 1);
}
else
{
include_space(size - i);
}
j = 0;
//loop, which is control the printing column operations
while (j <= (size / 2).toInt)
{
print("* ");
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
}
}``````

Output

`````` Height : 7

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

Height : 9

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

Height : 11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````
``````/*
Swift program
Print chevron shape of given size
*/
class MyPattern
{
//include space of given size
func include_space(_ size: Int)
{
//include double space in given size
var i: Int = 0;
while (i < size * 2)
{
print(" ", terminator: "");
i += 1;
}
}
//Print chevron shape star pattern of given size
func print_chevron_shape(_ size: Int)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
print("\n Height : ", 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)
{
if (i < size / 2)
{
self.include_space(i + 1);
}
else
{
self.include_space(size - i);
}
j = 0;
//loop, which is control the printing column operations
while (j <= size / 2)
{
print("* ", terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_chevron_shape(7);
obj.print_chevron_shape(9);
obj.print_chevron_shape(11);
}
main();``````

Output

`````` Height :  7

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

Height :  9

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

Height :  11

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *``````

Time Complexity Analysis

The time complexity of the algorithm can be analyzed as follows:

The outer loop runs for `size` iterations, which contributes O(size) to the time complexity.

The inner loop runs for `size/2` iterations, which contributes O(size) to the time complexity.

The `include_space` function also runs in O(size) time, as it prints spaces based on the input size.

Therefore, the overall time complexity of the code is O(size^2).

Finally

In this article, we discussed how to print a chevron shape of a given size using a simple algorithm. We provided the problem statement, explained the algorithm and pseudocode, and analyzed the time complexity of the code. By following the steps outlined in the article, you can implement the chevron shape printing functionality in any programming language. Understanding the algorithm and time complexity analysis allows you to optimize the code or modify it according to your requirements. Have fun experimenting with different chevron sizes and exploring variations of the pattern!

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.

Categories
Relative Post