Print V pattern
Here given code implementation process.
// C Program
// Display V pattern
#include <stdio.h>
//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
printf(" ");
}
}
void show_v(int size)
{
if(size < 2 )
{
return;
}
printf("Size : %d\n\n",size );
int side = (size*2)-3;
for (int i = 0; i < size; i++)
{
space(i);
printf("*");
space(side-i);
if(i < side)
{
printf("*");
}
side--;
printf("\n");
}
printf("\n");
}
int main()
{
//Test Case
show_v(5);
show_v(7);
show_v(3);
show_v(9);
return 0;
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// C++ Program
// Display V pattern
#include<iostream>
using namespace std;
class MyPattern {
public:
//include space
void space(int size) {
for (int i = 0; i < size; ++i) {
cout << " ";
}
}
void show_v(int size) {
if (size < 2) {
return;
}
cout << "Size : " << size << "\n\n";
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";
}
cout << "\n";
}
};
int main() {
MyPattern obj = MyPattern();
//Test Case
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
return 0;
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// Java Program
// Display V pattern
class MyPattern {
//include space
public void space(int size) {
for (int i = 0; i < size; ++i) {
System.out.print(" ");
}
}
public void show_v(int size) {
if (size < 2) {
return;
}
System.out.print("Size : "+size+"\n\n");
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");
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Case
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
}
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// C# Program
// Display V pattern
using System;
public class MyPattern {
//include space
public void space(int size) {
for (int i = 0; i < size; ++i) {
Console.Write(" ");
}
}
public void show_v(int size) {
if (size < 2) {
return;
}
Console.Write("Size : " + size + "\n\n");
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");
}
Console.Write("\n");
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
}
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
<?php
// Php Program
// Display V pattern
class MyPattern {
//include space
public function space($size) {
for ($i = 0; $i < $size; ++$i) {
echo(" ");
}
}
public function show_v($size) {
if ($size < 2) {
return;
}
echo("Size : ". $size ."\n\n");
$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");
}
echo("\n");
}
}
function main() {
$obj = new MyPattern();
//Test Case
$obj->show_v(5);
$obj->show_v(7);
$obj->show_v(3);
$obj->show_v(9);
}
main();
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// Node Js Program
// Display V pattern
class MyPattern {
//include space
space(size) {
for (var i = 0; i < size; ++i) {
process.stdout.write(" ");
}
}
show_v(size) {
if (size < 2) {
return;
}
process.stdout.write("Size : " + size + "\n\n");
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");
}
process.stdout.write("\n");
}
}
function main(args) {
var obj = new MyPattern();
//Test Case
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
}
main();
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
# Python 3 Program
# Display V pattern
class MyPattern :
# include space
def space(self, size) :
i = 0
while (i < size) :
print(" ", end = "")
i += 1
def show_v(self, size) :
if (size < 2) :
return
print("Size : ", size ,"\n\n", end = "")
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("\n", end = "")
i += 1
print("\n", end = "")
def main() :
obj = MyPattern()
obj.show_v(5)
obj.show_v(7)
obj.show_v(3)
obj.show_v(9)
if __name__ == "__main__":
main()
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
# Ruby Program
# Display V pattern
class MyPattern
# include space
def space(size)
i = 0
while (i < size)
print(" ")
i += 1
end
end
def show_v(size)
if (size < 2)
return
end
print("Size : ", size ,"\n\n")
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
print("\n")
end
end
def main()
obj = MyPattern.new()
obj.show_v(5)
obj.show_v(7)
obj.show_v(3)
obj.show_v(9)
end
main()
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// Scala Program
// Display V pattern
class MyPattern {
//include space
def space(size: Int): Unit = {
var i: Int = 0;
while (i < size) {
print(" ");
i += 1;
}
}
def show_v(size: Int): Unit = {
if (size < 2) {
return;
}
print("Size : " + size + "\n\n");
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;
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
}
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// Swift Program
// Display V pattern
class MyPattern {
//include space
func space(_ size: Int) {
var i: Int = 0;
while (i < size) {
print(" ", terminator: "");
i += 1;
}
}
func show_v(_ size: Int) {
if (size < 2) {
return;
}
print("Size : ", size ,"\n\n", terminator: "");
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;
}
print("\n", terminator: "");
}
}
func main() {
let obj: MyPattern = MyPattern();
obj.show_v(5);
obj.show_v(7);
obj.show_v(3);
obj.show_v(9);
}
main();
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
fn main() {
//Test Case
show_v(5);
show_v(7);
show_v(3);
show_v(9);
}
fn show_v(size: i32) {
if size < 2 {
return;
}
print!("Size : {}\n\n", size);
let mut side: i32 = (size * 2) - 3;
let mut i: i32 = 0;
while i < size {
space(i);
print!("*");
space(side - i);
if i < side {
print!("*");
}
side -= 1;
print!("\n");
i += 1;
}
print!("\n");
}
fn space(size: i32) {
let mut i: i32 = 0;
while i < size {
print!(" ");
i += 1;
}
}
Output
Size : 5
* *
* *
* *
* *
*
Size : 7
* *
* *
* *
* *
* *
* *
*
Size : 3
* *
* *
*
Size : 9
* *
* *
* *
* *
* *
* *
* *
* *
*
// C Program
// Display inverted v pattern
#include <stdio.h>
//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
printf(" ");
}
}
void print_inverted_v(int size)
{
if (size <= 2)
{
return;
}
printf("Size : %d\n\n", size);
for (int i = 0; i < size; i++)
{
space(size - i);
printf("*");
space(i + i - 1);
if (i != 0)
{
printf("*");
}
printf("\n");
}
printf("\n");
}
int main()
{
//Test Case
print_inverted_v(5);
print_inverted_v(7);
print_inverted_v(3);
print_inverted_v(9);
return 0;
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// Java Program
// Display inverted v pattern
class MyPattern
{
//include space
public void space(int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" ");
}
}
public void print_inverted_v(int size)
{
if (size <= 2)
{
return;
}
System.out.print("Size : " + size + "\n\n");
for (int i = 0; i < size; i++)
{
space(size - i);
System.out.print("*");
space(i + i - 1);
if (i != 0)
{
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
}
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// C++ Program
// Display inverted v pattern
#include<iostream>
using namespace std;
class MyPattern
{
public:
//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
cout << " ";
}
}
void print_inverted_v(int size)
{
if (size <= 2)
{
return;
}
cout << "Size : " << size << "\n\n";
for (int i = 0; i < size; i++)
{
this->space(size - i);
cout << "*";
this->space(i + i - 1);
if (i != 0)
{
cout << "*";
}
cout << "\n";
}
cout << "\n";
}
};
int main()
{
MyPattern obj = MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
return 0;
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// C# Program
// Display inverted v pattern
using System;
class MyPattern
{
//include space
public void space(int size)
{
for (int i = 0; i < size; i++)
{
Console.Write(" ");
}
}
public void print_inverted_v(int size)
{
if (size <= 2)
{
return;
}
Console.Write("Size : " + size + "\n\n");
for (int i = 0; i < size; i++)
{
space(size - i);
Console.Write("*");
space(i + i - 1);
if (i != 0)
{
Console.Write("*");
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
}
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
<?php
// Php Program
// Display inverted v pattern
class MyPattern
{
//include space
public function space($size)
{
for ($i = 0; $i < $size; ++$i)
{
echo(" ");
}
}
public function print_inverted_v($size)
{
if ($size <= 2)
{
return;
}
echo("Size : ". $size ."\n\n");
for ($i = 0; $i < $size; $i++)
{
$this->space($size - $i);
echo("*");
$this->space($i + $i - 1);
if ($i != 0)
{
echo("*");
}
echo("\n");
}
echo("\n");
}
}
function main()
{
$obj = new MyPattern();
//Test Case
$obj->print_inverted_v(5);
$obj->print_inverted_v(7);
$obj->print_inverted_v(3);
$obj->print_inverted_v(9);
}
main();
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// Node Js Program
// Display inverted v pattern
class MyPattern
{
//include space
space(size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" ");
}
}
print_inverted_v(size)
{
if (size <= 2)
{
return;
}
process.stdout.write("Size : " + size + "\n\n");
for (var i = 0; i < size; i++)
{
this.space(size - i);
process.stdout.write("*");
this.space(i + i - 1);
if (i != 0)
{
process.stdout.write("*");
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
}
function main(args)
{
var obj = new MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
}
main();
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
# Python 3 Program
# Display inverted v pattern
class MyPattern :
# include space
def space(self, size) :
i = 0
while (i < size) :
print(end = " ")
i += 1
def print_inverted_v(self, size) :
if (size <= 2) :
return
print("Size : ", size ,"\n\n", end = "")
i = 0
while (i < size) :
self.space(size - i)
print(end = "*")
self.space(i + i - 1)
if (i != 0) :
print(end = "*")
print(end = "\n")
i += 1
print(end = "\n")
def main() :
obj = MyPattern()
# Test Case
obj.print_inverted_v(5)
obj.print_inverted_v(7)
obj.print_inverted_v(3)
obj.print_inverted_v(9)
if __name__ == "__main__" : main()
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
# Ruby Program
# Display inverted v pattern
class MyPattern
# include space
def space(size)
i = 0
while (i < size)
print(" ")
i += 1
end
end
def print_inverted_v(size)
if (size <= 2)
return
end
print("Size : ", size ,"\n\n")
i = 0
while (i < size)
self.space(size - i)
print("*")
self.space(i + i - 1)
if (i != 0)
print("*")
end
print("\n")
i += 1
end
print("\n")
end
end
def main()
obj = MyPattern.new()
# Test Case
obj.print_inverted_v(5)
obj.print_inverted_v(7)
obj.print_inverted_v(3)
obj.print_inverted_v(9)
end
main()
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// Scala Program
// Display inverted v pattern
class MyPattern
{
//include space
def space(size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" ");
i += 1;
}
}
def print_inverted_v(size: Int): Unit = {
if (size <= 2)
{
return;
}
print("Size : " + size + "\n\n");
var i: Int = 0;
while (i < size)
{
space(size - i);
print("*");
space(i + i - 1);
if (i != 0)
{
print("*");
}
print("\n");
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
}
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
// Swift Program
// Display inverted v pattern
class MyPattern
{
//include space
func space(_ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", terminator: "");
i += 1;
}
}
func print_inverted_v(_ size: Int)
{
if (size <= 2)
{
return;
}
print("Size : ", size ,"\n");
var i: Int = 0;
while (i < size)
{
self.space(size - i);
print(terminator: "*");
self.space(i + i - 1);
if (i != 0)
{
print(terminator: "*");
}
print(terminator: "\n");
i += 1;
}
print(terminator: "\n");
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Case
obj.print_inverted_v(5);
obj.print_inverted_v(7);
obj.print_inverted_v(3);
obj.print_inverted_v(9);
}
main();
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
fn main()
{
//Test Case
print_inverted_v(5);
print_inverted_v(7);
print_inverted_v(3);
print_inverted_v(9);
}
fn print_inverted_v(size: i32)
{
if size <= 2
{
return;
}
print!("Size : {}\n\n", size);
let mut i: i32 = 0;
while i < size
{
space(size - i);
print!("*");
space(i + i - 1);
if i != 0
{
print!("*");
}
print!("\n");
i += 1;
}
print!("\n");
}
fn space(size: i32)
{
let mut i: i32 = 0;
while i < size
{
print!(" ");
i += 1;
}
}
Output
Size : 5
*
* *
* *
* *
* *
Size : 7
*
* *
* *
* *
* *
* *
* *
Size : 3
*
* *
* *
Size : 9
*
* *
* *
* *
* *
* *
* *
* *
* *
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