Print B star pattern
A B star pattern is a pattern of characters that resembles the letter B with a star inside it. It is a popular pattern used in programming exercises and is often used to demonstrate the use of loops and conditional statements.
Here is an example of a B star pattern:
* * * *
* *
* *
* * * *
* *
* *
* * * *
In this pattern, the B shape is created using asterisks (*) and spaces, and the star inside the B is also made of asterisks. The pattern consists of rows and columns.
To create this pattern, you can use nested loops to iterate through the rows and columns of the pattern and print the appropriate character at each position. You can also use conditional statements to check if the current position should be filled with an asterisk or a space.
Overall, the B star pattern is a simple yet interesting pattern that can help you practice your programming skills.
Here given code implementation process.
//C Program
//Print B 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)
{
//Add Star
printf("*");
}
else
{
//Add Space
printf(" ");
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
//Print 'B' star pattern of given size
void print_b(int height)
{
if (height < 3 || height % 2 == 0)
{
return;
}
printf("\nHeight : %d \n\n", height);
for (int i = 0; i < height; ++i)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
printf("*");
//include intermediate space
print_space(height - 1);
//include star in last column
printf("*");
print_space(2);
}
printf("\n");
}
}
int main()
{
//Simple test
print_b(7);
print_b(9);
print_b(3);
print_b(13);
return 0;
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
Java Program
Print B 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)
{
//Add Star
System.out.print("*");
}
else
{
//Add Space
System.out.print(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
//Print 'B' star pattern of given size
public void print_b(int height)
{
if (height < 3 || height % 2 == 0)
{
return;
}
System.out.print("\nHeight : " + height + " \n\n");
for (int i = 0; i < height; ++i)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
System.out.print("*");
//include intermediate space
print_space(height - 1);
//include star in last column
System.out.print("*");
print_space(2);
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
C++ Program
Print B 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 'B' star pattern of given size
void print_b(int height)
{
if (height < 3 || height % 2 == 0)
{
return;
}
cout << "\nHeight : " << height << " \n\n";
for (int i = 0; i < height; ++i)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
this->print_star(height);
this->print_space(3);
}
else
{
cout << "*";
//include intermediate space
this->print_space(height - 1);
cout << "*";
this->print_space(2);
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
return 0;
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
C# Program
Print B 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)
{
//Add Star
Console.Write("*");
}
else
{
//Add Space
Console.Write(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
Console.Write(" ");
}
}
//Print 'B' star pattern of given size
public void print_b(int height)
{
if (height < 3 || height % 2 == 0)
{
return;
}
Console.Write("\nHeight : " + height + " \n\n");
for (int i = 0; i < height; i++)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
Console.Write("*");
//include intermediate space
print_space(height - 1);
//include star in last column
Console.Write("*");
print_space(2);
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
<?php
/*
Php Program
Print B star pattern
*/
class MyPattern
{
//Include star of given size
public function print_star($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
if ($counter % 2 == 0)
{
//Add Star
echo("*");
}
else
{
//Add Space
echo(" ");
}
}
}
//Display space of given size
public function print_space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add space
echo(" ");
}
}
//Print 'B' star pattern of given size
public function print_b($height)
{
if ($height < 3 || $height % 2 == 0)
{
return;
}
echo("\nHeight : ". $height ." \n\n");
for ($i = 0; $i < $height; ++$i)
{
if ($i == 0 || $i == intval($height / 2) || $i == $height - 1)
{
//Condition which is print the horizontal star
$this->print_star($height);
$this->print_space(3);
}
else
{
//This is first star of column zero
echo("*");
//include intermediate space
$this->print_space($height - 1);
//include star in last column
echo("*");
$this->print_space(2);
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_b(7);
$obj->print_b(9);
$obj->print_b(3);
$obj->print_b(13);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
Node Js Program
Print B 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)
{
//Add Star
process.stdout.write("*");
}
else
{
//Add Space
process.stdout.write(" ");
}
}
}
//Display space of given size
print_space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
process.stdout.write(" ");
}
}
//Print 'B' star pattern of given size
print_b(height)
{
if (height < 3 || height % 2 == 0)
{
return;
}
process.stdout.write("\nHeight : " + height + " \n\n");
for (var i = 0; i < height; ++i)
{
if (i == 0 || i == parseInt(height / 2) || i == height - 1)
{
//Condition which is print the horizontal star
this.print_star(height);
this.print_space(3);
}
else
{
//This is first star of column zero
process.stdout.write("*");
//include intermediate space
this.print_space(height - 1);
//include star in last column
process.stdout.write("*");
this.print_space(2);
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
# Python 3 Program
# Print B star pattern
class MyPattern :
# Include star of given size
def print_star(self, size) :
counter = 0
while (counter < size) :
if (counter % 2 == 0) :
# Add Star
print("*", end = "")
else :
# Add Space
print(" ", end = "")
counter += 1
# Display space of given size
def print_space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Print 'B' star pattern of given size
def print_b(self, height) :
if (height < 3 or height % 2 == 0) :
return
print("\nHeight : ", height ," \n")
i = 0
while (i < height) :
if (i == 0 or i == int(height / 2) or i == height - 1) :
# Condition which is print the horizontal star
self.print_star(height)
self.print_space(3)
else :
print("*", end = "")
# include intermediate space
self.print_space(height - 1)
print("*", end = "")
self.print_space(2)
print(end = "\n")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_b(7)
obj.print_b(9)
obj.print_b(3)
obj.print_b(13)
if __name__ == "__main__": main()
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
# Ruby Program
# Print B star pattern
class MyPattern
# Include star of given size
def print_star(size)
counter = 0
while (counter < size)
if (counter % 2 == 0)
# Add Star
print("*")
else
# Add Space
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 'B' star pattern of given size
def print_b(height)
if (height < 3 || height % 2 == 0)
return
end
print("\nHeight : ", height ," \n\n")
i = 0
while (i < height)
if (i == 0 || i == height / 2 || i == height - 1)
# Condition which is print the horizontal star
self.print_star(height)
self.print_space(3)
else
print("*")
# include intermediate space
self.print_space(height - 1)
print("*")
self.print_space(2)
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_b(7)
obj.print_b(9)
obj.print_b(3)
obj.print_b(13)
end
main()
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
Scala Program
Print B 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)
{
//Add Star
print("*");
}
else
{
//Add Space
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 'B' star pattern of given size
def print_b(height: Int): Unit = {
if (height < 3 || height % 2 == 0)
{
return;
}
print("\nHeight : " + height + " \n\n");
var i: Int = 0;
while (i < height)
{
if (i == 0 || i == (height / 2).toInt || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
print("*");
//include intermediate space
print_space(height - 1);
print("*");
print_space(2);
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
/*
Swift Program
Print B 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)
{
//Add Star
print("*", terminator: "");
}
else
{
//Add Space
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 "B" star pattern of given size
func print_b(_ height: Int)
{
if (height < 3 || height % 2 == 0)
{
return;
}
print("\nHeight : ", height ," \n\n", terminator: "");
var i: Int = 0;
while (i < height)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
self.print_star(height);
self.print_space(3);
}
else
{
print(terminator: "*");
//include intermediate space
self.print_space(height - 1);
print(terminator: "*");
self.print_space(2);
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_b(7);
obj.print_b(9);
obj.print_b(3);
obj.print_b(13);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* * * *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* * * * *
Height : 3
* *
* *
* *
Height : 13
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
//C Program
//Print B star pattern set B
#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)
{
//Add Star
printf("*");
}
else
{
//Add Space
printf(" ");
}
}
}
//Display space of given size
void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
printf(" ");
}
}
//Print 'B' star pattern of given size
void print_b(int height, int cols)
{
if (height < 3 || height % 2 == 0)
{
return;
}
printf("\nHeight : %d COLS : %d\n\n", height, cols);
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < cols; j++)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
printf("*");
//include intermediate space
print_space(height - 1);
//include star in last column
printf("*");
print_space(2);
}
}
printf("\n");
}
}
int main()
{
//Simple test
print_b(7, 3);
print_b(9, 2);
print_b(5, 3);
return 0;
}
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
Java Program
Print B star pattern Set B
*/
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)
{
//Add Star
System.out.print("*");
}
else
{
//Add Space
System.out.print(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
System.out.print(" ");
}
}
//Print 'B' star pattern of given size
public void print_b(int height, int cols)
{
if (height < 3 || height % 2 == 0)
{
return;
}
System.out.print("\nHeight : " + height + " COLS : " + cols + " \n\n");
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < cols; j++)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
System.out.print("*");
//include intermediate space
print_space(height - 1);
//include star in last column
System.out.print("*");
print_space(2);
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
}
}
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
C++ Program
Print B star pattern Set B
*/
#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 'B' star pattern of given size
void print_b(int height, int cols)
{
if (height < 3 || height % 2 == 0)
{
return;
}
cout << "\nHeight : " << height << " COLS : " << cols << " \n\n";
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < cols; j++)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
this->print_star(height);
this->print_space(3);
}
else
{
cout << "*";
//include intermediate space
this->print_space(height - 1);
cout << "*";
this->print_space(2);
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
return 0;
}
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
C# Program
Print B star pattern Set B
*/
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)
{
//Add Star
Console.Write("*");
}
else
{
//Add Space
Console.Write(" ");
}
}
}
//Display space of given size
public void print_space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
Console.Write(" ");
}
}
//Print 'B' star pattern of given size
public void print_b(int height, int cols)
{
if (height < 3 || height % 2 == 0)
{
return;
}
Console.Write("\nHeight : " + height + " COLS : " + cols + " \n\n");
for (int i = 0; i < height; i++)
{
for (int j = 0; j < cols; j++)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
//This is first star of column zero
Console.Write("*");
//include intermediate space
print_space(height - 1);
//include star in last column
Console.Write("*");
print_space(2);
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
}
}
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
<?php
/*
Php Program
Print B star pattern Set B
*/
class MyPattern
{
//Include star of given size
public function print_star($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
if ($counter % 2 == 0)
{
//Add Star
echo("*");
}
else
{
//Add Space
echo(" ");
}
}
}
//Display space of given size
public function print_space($size)
{
$counter = 0;
for ($counter = 0; $counter < $size; $counter++)
{
//Add space
echo(" ");
}
}
//Print 'B' star pattern of given size
public function print_b($height, $cols)
{
if ($height < 3 || $height % 2 == 0)
{
return;
}
echo("\nHeight : ". $height ." COLS : ". $cols ." \n\n");
for ($i = 0; $i < $height; ++$i)
{
for ($j = 0; $j < $cols; $j++)
{
if ($i == 0 || $i == intval($height / 2) || $i == $height - 1)
{
//Condition which is print the horizontal star
$this->print_star($height);
$this->print_space(3);
}
else
{
//This is first star of column zero
echo("*");
//include intermediate space
$this->print_space($height - 1);
//include star in last column
echo("*");
$this->print_space(2);
}
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_b(7, 3);
$obj->print_b(9, 2);
$obj->print_b(5, 3);
}
main();
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
Node Js Program
Print B star pattern Set B
*/
class MyPattern
{
//Include star of given size
print_star(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter % 2 == 0)
{
//Add Star
process.stdout.write("*");
}
else
{
//Add Space
process.stdout.write(" ");
}
}
}
//Display space of given size
print_space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
//Add space
process.stdout.write(" ");
}
}
//Print 'B' star pattern of given size
print_b(height, cols)
{
if (height < 3 || height % 2 == 0)
{
return;
}
process.stdout.write("\nHeight : " + height + " COLS : " + cols + " \n\n");
for (var i = 0; i < height; ++i)
{
for (var j = 0; j < cols; j++)
{
if (i == 0 || i == parseInt(height / 2) || i == height - 1)
{
//Condition which is print the horizontal star
this.print_star(height);
this.print_space(3);
}
else
{
//This is first star of column zero
process.stdout.write("*");
//include intermediate space
this.print_space(height - 1);
//include star in last column
process.stdout.write("*");
this.print_space(2);
}
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
}
main();
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
# Python 3 Program
# Print B star pattern Set B
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 'B' star pattern of given size
def print_b(self, height, cols) :
if (height < 3 or height % 2 == 0) :
return
print("\nHeight : ", height ," COLS : ", cols ," \n")
i = 0
j = 0
while (i < height) :
j = 0
while (j < cols) :
if (i == 0 or i == int(height / 2) or i == height - 1) :
# Condition which is print the horizontal star
self.print_star(height)
self.print_space(3)
else :
print(end = "*")
# include intermediate space
self.print_space(height - 1)
print(end = "*")
self.print_space(2)
j += 1
print(end = "\n")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_b(7, 3)
obj.print_b(9, 2)
obj.print_b(5, 3)
if __name__ == "__main__": main()
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
# Ruby Program
# Print B star pattern Set B
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 'B' star pattern of given size
def print_b(height, cols)
if (height < 3 || height % 2 == 0)
return
end
print("\nHeight : ", height ," COLS : ", cols ," \n\n")
i = 0
j = 0
while (i < height)
j = 0
while (j < cols)
if (i == 0 || i == height / 2 || i == height - 1)
# Condition which is print the horizontal star
self.print_star(height)
self.print_space(3)
else
print("*")
# include intermediate space
self.print_space(height - 1)
print("*")
self.print_space(2)
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_b(7, 3)
obj.print_b(9, 2)
obj.print_b(5, 3)
end
main()
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
Scala Program
Print B star pattern Set B
*/
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 'B' star pattern of given size
def print_b(height: Int, cols: Int): Unit = {
if (height < 3 || height % 2 == 0)
{
return;
}
print("\nHeight : " + height + " COLS : " + cols + " \n\n");
var i: Int = 0;
var j: Int = 0;
while (i < height)
{
j = 0;
while (j < cols)
{
if (i == 0 || i == (height / 2).toInt || i == height - 1)
{
//Condition which is print the horizontal star
print_star(height);
print_space(3);
}
else
{
print("*");
//include intermediate space
print_space(height - 1);
print("*");
print_space(2);
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
}
}
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
/*
Swift Program
Print B star pattern Set B
*/
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 "B" star pattern of given size
func print_b(_ height: Int, _ cols: Int)
{
if (height < 3 || height % 2 == 0)
{
return;
}
print("\nHeight : ", height ," COLS : ", cols ," \n");
var i: Int = 0;
var j: Int = 0;
while (i < height)
{
j = 0;
while (j < cols)
{
if (i == 0 || i == height / 2 || i == height - 1)
{
//Condition which is print the horizontal star
self.print_star(height);
self.print_space(3);
}
else
{
print(terminator: "*");
//include intermediate space
self.print_space(height - 1);
print(terminator: "*");
self.print_space(2);
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_b(7, 3);
obj.print_b(9, 2);
obj.print_b(5, 3);
}
main();
Output
Height : 7 COLS : 3
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
Height : 9 COLS : 2
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
Height : 5 COLS : 3
* * * * * * * * *
* * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * * *
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