Print chevron shape of given size
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 header file
#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
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
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