Print the R pattern
In this article, we will discuss a program to print the R pattern using stars (*) and spaces. The program is implemented in C language and follows a simple algorithm to generate the desired pattern.
Problem Statement
The problem is to print the letter 'R' using stars in a pattern. The pattern should be formed by a combination of stars and spaces. The height of the 'R' pattern is provided as input to the program.
The program should satisfy the following conditions:
- The height of the pattern should be an odd number greater than or equal to 3.
- The pattern should resemble the uppercase letter 'R'.
Example
Let's consider an example to understand the problem and its solution.
If the height of the pattern is 7, the output should be:
* * * * * * * * * * * * * * * * * *
Algorithm
- Start the program.
- Define a function 'print_star' that takes the size as an input parameter.
- Initialize a counter to 0.
- Run a loop from 0 to size-1: a. Check if the counter is even: i. If true, print a star (*) character. ii. If false, print a space character. b. Increment the counter by 1.
- Define another function 'print_space' that takes the size as an input parameter.
- Initialize a counter to 0.
- Run a loop from 0 to size-1: a. Print a space character. b. Increment the counter by 1.
- Define a function 'show_r' that takes the height as an input parameter.
- Check if the height is less than 3 or an even number: a. If true, return from the function.
- Print the height value.
- Run a loop from 0 to height-1: a. Check if the current index is either 0 or height/2: i. If true, call the 'print_star' function passing the height as a parameter. - This will print a row of stars in the horizontal direction. ii. Call the 'print_space' function passing 3 as a parameter. - This will print 3 spaces after the row of stars. b. Else: i. Print a star () character. ii. Call the 'print_space' function passing height-1 as a parameter. - This will print spaces after the boundary star. iii. Print a star () character. iv. Call the 'print_space' function passing 2 as a parameter. - This will print 2 spaces after the boundary star.
- Print a new line.
- Implement the main function.
- Call the 'show_r' function with different height values to test the program.
- End the program.
Code Solution
Here given code implementation process.
//C Program
//Print R 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 'R' star pattern of given size
void show_r(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)
{
//when printed the star in horizontal direction
print_star(height);
print_space(3);
}
else
{
//print boundary elements
printf("*");
print_space(height - 1);
printf("*");
print_space(2);
}
printf("\n");
}
}
int main()
{
//Simple test
show_r(7);
show_r(9);
show_r(3);
show_r(15);
return 0;
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
Java Program
Print R 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 'R' star pattern of given size
public void show_r(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)
{
//when printed the star in horizontal direction
print_star(height);
print_space(3);
}
else
{
//print boundary elements
System.out.print("*");
print_space(height - 1);
System.out.print("*");
print_space(2);
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
C++ Program
Print R 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 'R' star pattern of given size
void show_r(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)
{
//when printed the star in horizontal direction
this->print_star(height);
this->print_space(3);
}
else
{
cout << "*";
this->print_space(height - 1);
cout << "*";
this->print_space(2);
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
return 0;
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
C# Program
Print R 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 'R' star pattern of given size
public void show_r(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)
{
//when printed the star in horizontal direction
print_star(height);
print_space(3);
}
else
{
//print boundary elements
Console.Write("*");
print_space(height - 1);
Console.Write("*");
print_space(2);
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
<?php
/*
Php Program
Print R 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 'R' star pattern of given size
public function show_r($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))
{
//when printed the star in horizontal direction
$this->print_star($height);
$this->print_space(3);
}
else
{
//print boundary elements
echo("*");
$this->print_space($height - 1);
echo("*");
$this->print_space(2);
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->show_r(7);
$obj->show_r(9);
$obj->show_r(3);
$obj->show_r(15);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
Node Js Program
Print R 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 'R' star pattern of given size
show_r(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))
{
//when printed the star in horizontal direction
this.print_star(height);
this.print_space(3);
}
else
{
//print boundary elements
process.stdout.write("*");
this.print_space(height - 1);
process.stdout.write("*");
this.print_space(2);
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
# Python 3 Program
# Print R star pattern
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 'R' star pattern of given size
def show_r(self, height) :
if (height < 3 or height % 2 == 0) :
return
print("\nHeight : ", height ," \n\n", end = "")
i = 0
while (i < height) :
if (i == 0 or i == int(height / 2)) :
# when printed the star in horizontal direction
self.print_star(height)
self.print_space(3)
else :
print("*", end = "")
self.print_space(height - 1)
print("*", end = "")
self.print_space(2)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.show_r(7)
obj.show_r(9)
obj.show_r(3)
obj.show_r(15)
if __name__ == "__main__": main()
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
# Ruby Program
# Print R star pattern
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 'R' star pattern of given size
def show_r(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)
# when printed the star in horizontal direction
self.print_star(height)
self.print_space(3)
else
print("*")
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.show_r(7)
obj.show_r(9)
obj.show_r(3)
obj.show_r(15)
end
main()
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
Scala Program
Print R 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)
{
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 'R' star pattern of given size
def show_r(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)
{
//when printed the star in horizontal direction
print_star(height);
print_space(3);
}
else
{
print("*");
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.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
}
}
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
/*
Swift Program
Print R 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)
{
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 "R" star pattern of given size
func show_r(_ 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)
{
//when printed the star in horizontal direction
self.print_star(height);
self.print_space(3);
}
else
{
print("*", terminator: "");
self.print_space(height - 1);
print("*", terminator: "");
self.print_space(2);
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.show_r(7);
obj.show_r(9);
obj.show_r(3);
obj.show_r(15);
}
main();
Output
Height : 7
* * * *
* *
* *
* * * *
* *
* *
* *
Height : 9
* * * * *
* *
* *
* *
* * * * *
* *
* *
* *
* *
Height : 3
* *
* *
* *
Height : 15
* * * * * * * *
* *
* *
* *
* *
* *
* *
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
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