Print Right Arrow Patterns
In this article, we will discuss the problem of printing right arrow patterns using a simple C program. The program takes a size as input and generates a right arrow pattern based on that size. We will explain the problem statement, provide a suitable example, present the algorithm and pseudocode with explanations, and finally, discuss the resultant output and the time complexity of the code.
Problem Statement
The problem is to write a program that prints a right arrow pattern using asterisks (*) and spaces. The pattern consists of two parts: the top part and the bottom part. The top part of the arrow starts with one asterisk and adds an extra asterisk in each line until it reaches the given size. The bottom part starts with the size minus one asterisks and decreases one asterisk in each line until it reaches one asterisk. The pattern should be centered, and the number of spaces before each line should be equal to its line number.
Example
Let's take an example to understand the problem better. Suppose we want to generate a right arrow pattern with a size of 4.
The expected pattern would be:
* ** *** **** *** ** *
In this example, the top part of the arrow starts with one asterisk and increases by one asterisk in each line. The bottom part starts with three asterisks and decreases by one asterisk in each line.
Algorithm and Pseudocode
Based on the problem statement, we can design the following algorithm:
- Define a function to print a given number of spaces.
- Define a function to print a given number of asterisks.
- Define a function to display the right arrow pattern.
- Inside the function to display the right arrow pattern:
- Print the size of the pattern.
- Loop from 0 to size-1:
- Call the space function with the current iteration index.
- Call the print_star function with the current iteration index.
- Print a new line.
- Loop from 0 to size-1:
- Call the space function with size minus the current iteration index.
- Call the print_star function with size minus the current iteration index.
- Print a new line.
- Inside the main function:
- Call the right_arrow function with different sizes to test the pattern.
Here is the pseudocode representation of the algorithm:
function space(size):
for counter from 0 to size:
print a space
function print_star(size):
for counter from 0 to size:
print an asterisk
function right_arrow(size):
print "Size: " + size
for i from 0 to size-1:
space(i)
print_star(i)
print a new line
for i from 0 to size-1:
space(size-i)
print_star(size-i)
print a new line
main():
right_arrow(3)
right_arrow(4)
right_arrow(7)
Code Solution
Here given code implementation process.
//C Program
//Print Right Arrow Pattern
#include <stdio.h>
#include <stdlib.h>
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf(" ");
}
}
/*Include Space of given size*/
void print_star(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf("*");
}
}
//Display the right arrow of given size
void right_arrow(int size) {
printf("\n Size : %d \n", size);
int i=0;
for(i=0;i<size;i++){
space(i);
print_star(i);
printf("\n");
}
for(i=0;i<size;i++){
space(size-i);
print_star(size-i);
printf("\n");
}
}
int main() {
//Test Cases
right_arrow(3);
right_arrow(4);
right_arrow(7);
return 0;
}
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
C++ Program
Print right Arrow Pattern
*/
#include<iostream>
using namespace std;
class MyPattern {
public:
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
cout << " ";
}
}
/*Include Space of given size*/
void print_star(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
cout << "*";
}
}
//Display the right arrow of given size
void right_arrow(int size) {
cout << "\n Size : " << size << " \n";
int i = 0;
for (i = 0; i < size; i++) {
this->space(i);
this->print_star(i);
cout << "\n";
}
for (i = 0; i < size; i++) {
this->space(size - i);
this->print_star(size - i);
cout << "\n";
}
}
};
int main() {
MyPattern obj = MyPattern();
//Test Cases
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
return 0;
}
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
Java Program
Print right Arrow Pattern
*/
public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
System.out.print(" ");
}
}
/*Include Space of given size*/
public void print_star(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
System.out.print("*");
}
}
//Display the right arrow of given size
public void right_arrow(int size) {
System.out.print("\n Size : "+size+" \n");
int i=0;
for(i=0;i<size;i++){
space(i);
print_star(i);
System.out.print("\n");
}
for(i=0;i<size;i++){
space(size-i);
print_star(size-i);
System.out.print("\n");
}
}
public static void main(String[] args) {
MyPattern obj = new MyPattern();
//Test Cases
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
}
}
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
C# Program
Print right Arrow Pattern
*/
using System;
public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write(" ");
}
}
/*Include Space of given size*/
public void print_star(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write("*");
}
}
//Display the right arrow of given size
public void right_arrow(int size) {
Console.Write("\n Size : " + size + " \n");
int i = 0;
for (i = 0; i < size; i++) {
space(i);
print_star(i);
Console.Write("\n");
}
for (i = 0; i < size; i++) {
space(size - i);
print_star(size - i);
Console.Write("\n");
}
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
}
}
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
<?php
/*
Php Program
Print right Arrow Pattern
*/
class MyPattern {
/*Include Space of given size*/
public function space($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
//Add space
echo(" ");
}
}
/*Include Space of given size*/
public function print_star($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
//Add space
echo("*");
}
}
//Display the right arrow of given size
public function right_arrow($size) {
echo("\n Size : ". $size ." \n");
$i = 0;
for ($i = 0; $i < $size; $i++) {
$this->space($i);
$this->print_star($i);
echo("\n");
}
for ($i = 0; $i < $size; $i++) {
$this->space($size - $i);
$this->print_star($size - $i);
echo("\n");
}
}
}
function main() {
$obj = new MyPattern();
//Test Cases
$obj->right_arrow(3);
$obj->right_arrow(4);
$obj->right_arrow(7);
}
main();
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
Node Js Program
Print right Arrow Pattern
*/
class MyPattern {
/*Include Space of given size*/
space(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
process.stdout.write(" ");
}
}
/*Include Space of given size*/
print_star(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
process.stdout.write("*");
}
}
//Display the right arrow of given size
right_arrow(size) {
process.stdout.write("\n Size : " + size + " \n");
var i = 0;
for (i = 0; i < size; i++) {
this.space(i);
this.print_star(i);
process.stdout.write("\n");
}
for (i = 0; i < size; i++) {
this.space(size - i);
this.print_star(size - i);
process.stdout.write("\n");
}
}
}
function main(args) {
var obj = new MyPattern();
//Test Cases
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
}
main();
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
# Python 3 Program
# Print right Arrow Pattern
class MyPattern :
# Include Space of given size
def space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Include Space of given size
def print_star(self, size) :
counter = 0
while (counter < size) :
print("*", end = "")
counter += 1
# Display the right arrow of given size
def right_arrow(self, size) :
print("\n Size : ", size ," \n", end = "")
i = 0
while (i < size) :
self.space(i)
self.print_star(i)
print("\n", end = "")
i += 1
i = 0
while (i < size) :
self.space(size - i)
self.print_star(size - i)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
obj.right_arrow(3)
obj.right_arrow(4)
obj.right_arrow(7)
if __name__ == "__main__":
main()
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
# Ruby Program
# Print right Arrow Pattern
class MyPattern
# Include Space of given size
def space(size)
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
# Include Space of given size
def print_star(size)
counter = 0
while (counter < size)
print("*")
counter += 1
end
end
# Display the right arrow of given size
def right_arrow(size)
print("\n Size :", size ," \n")
i = 0
while (i < size)
self.space(i)
self.print_star(i)
print("\n")
i += 1
end
i = 0
while (i < size)
self.space(size - i)
self.print_star(size - i)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.right_arrow(3)
obj.right_arrow(4)
obj.right_arrow(7)
end
main()
Output
Size :3
*
**
***
**
*
Size :4
*
**
***
****
***
**
*
Size :7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
Scala Program
Print right Arrow Pattern
*/
class MyPattern {
/*Include Space of given size*/
def space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size) {
print(" ");
counter += 1;
}
}
/*Include Space of given size*/
def print_star(size: Int): Unit = {
var counter: Int = 0;
while (counter < size) {
print("*");
counter += 1;
}
}
//Display the right arrow of given size
def right_arrow(size: Int): Unit = {
print("\n Size : " + size + " \n");
var i: Int = 0;
while (i < size) {
this.space(i);
this.print_star(i);
print("\n");
i += 1;
}
i = 0;
while (i < size) {
this.space(size - i);
this.print_star(size - i);
print("\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyPattern = new MyPattern();
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
}
}
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
/*
Swift Program
Print right Arrow Pattern
*/
class MyPattern {
/*Include Space of given size*/
func space(_ size: Int) {
var counter = 0;
while (counter < size) {
print(" ", terminator: "");
counter += 1;
}
}
/*Include Space of given size*/
func print_star(_ size: Int) {
var counter = 0;
while (counter < size) {
print("*", terminator: "");
counter += 1;
}
}
//Display the right arrow of given size
func right_arrow(_ size: Int) {
print("\n Size : ", size ," \n", terminator: "");
var i = 0;
while (i < size) {
self.space(i);
self.print_star(i);
print("\n", terminator: "");
i += 1;
}
i = 0;
while (i < size) {
self.space(size - i);
self.print_star(size - i);
print("\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj = MyPattern();
obj.right_arrow(3);
obj.right_arrow(4);
obj.right_arrow(7);
}
main();
Output
Size : 3
*
**
***
**
*
Size : 4
*
**
***
****
***
**
*
Size : 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
The output matches the expected right arrow patterns for the given sizes. The patterns are centered and consist of asterisks and spaces as defined in the problem statement.
Time Complexity
The time complexity of the program is determined by the loops used to print spaces and asterisks in the right arrow pattern. Both loops iterate from 0 to the given size, so the time complexity is O(size). As the size increases, the time taken to generate the pattern will increase linearly.
Print Variant Right Arrow Patterns
The problem is to design a pattern that represents right arrows of varying sizes using a combination of greater than signs (>) and hyphens (-). Each arrow consists of two parts: the top part and the bottom part. The top part starts with one greater than sign and gradually increases in size. The bottom part starts with the maximum size and gradually decreases. The arrows are centered, and the number of spaces before each line should be equal to its line number.
Examples
Let's examine a few examples to understand the problem better.
Example 1 (Size: 3)
> > >> >> >-> >-> >> >> > >
Double right arrow Solution
//C Program
//Print Double Right Arrow Pattern
#include <stdio.h>
#include <stdlib.h>
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf(" ");
}
}
/*Include Symbol*/
void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if(counter==0 || counter+1 == size)
{
printf(">");
}
else
{
printf("-");
}
}
}
//Display the double right arrow of given size
void double_right_arrow(int size) {
printf("\n Size : %d \n", size);
int i=0;
//Display the result of top half shell
for(i=0;i<size;i++){
space(i);
print_symbol(i);
space(size+(size/2)-i);
print_symbol(i);
printf("\n");
}
//Display the result of bottom half shell
for(i=0;i<size;i++){
space(size-i);
print_symbol(size-i);
space(i+(size/2));
print_symbol(size-i);
printf("\n");
}
}
int main() {
//Test Cases
double_right_arrow(3);
double_right_arrow(6);
double_right_arrow(7);
return 0;
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// C++ Program
// Print Double Right Arrow Pattern
#include<iostream>
using namespace std;
class MyPattern {
public:
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
cout << " ";
}
}
/*Include Symbol*/
void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
cout << ">";
} else {
cout << "-";
}
}
}
//Display the double right arrow of given size
void double_right_arrow(int size) {
cout << "\n Size : " << size << " \n";
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
this->space(i);
this->print_symbol(i);
this->space(size + (size / 2) - i);
this->print_symbol(i);
cout << "\n";
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
this->space(size - i);
this->print_symbol(size - i);
this->space(i + (size / 2));
this->print_symbol(size - i);
cout << "\n";
}
}
};
int main() {
MyPattern obj = MyPattern();
//Test Case
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
return 0;
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// Java Program
// Print Double Right Arrow Pattern
class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
System.out.print(" ");
}
}
/*Include Symbol*/
public void print_symbol(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
if (counter == 0 || counter + 1 == size)
{
System.out.print(">");
}
else
{
System.out.print("-");
}
}
}
//Display the double right arrow of given size
public void double_right_arrow(int size)
{
System.out.print("\n Size : "+size+" \n");
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++)
{
space(i);
print_symbol(i);
space(size + (size / 2) - i);
print_symbol(i);
System.out.print("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++)
{
space(size - i);
print_symbol(size - i);
space(i + (size / 2));
print_symbol(size - i);
System.out.print("\n");
}
}
public static void main(String[] args) {
MyPattern obj = new MyPattern();
//Test Case
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
}
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// C# Program
// Print Double Right Arrow Pattern
using System;
public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write(" ");
}
}
/*Include Symbol*/
public void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
Console.Write(">");
} else {
Console.Write("-");
}
}
}
//Display the double right arrow of given size
public void double_right_arrow(int size) {
Console.Write("\n Size : " + size + " \n");
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
space(i);
print_symbol(i);
space(size + (size / 2) - i);
print_symbol(i);
Console.Write("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
space(size - i);
print_symbol(size - i);
space(i + (size / 2));
print_symbol(size - i);
Console.Write("\n");
}
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
}
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
<?php
// Php Program
// Print Double Right Arrow Pattern
class MyPattern {
/*Include Space of given size*/
public function space($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
//Add space
echo(" ");
}
}
/*Include Symbol*/
public function print_symbol($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
if ($counter == 0 ||
$counter + 1 == $size) {
echo(">");
} else {
echo("-");
}
}
}
//Display the double right arrow of given size
public function double_right_arrow($size) {
echo("\n Size : ". $size ." \n");
$i = 0;
//Display the result of top half shell
for ($i = 0; $i < $size; $i++) {
$this->space($i);
$this->print_symbol($i);
$this->space($size + (intval($size / 2)) - $i);
$this->print_symbol($i);
echo("\n");
}
//Display the result of bottom half shell
for ($i = 0; $i < $size; $i++) {
$this->space($size - $i);
$this->print_symbol($size - $i);
$this->space($i + (intval($size / 2)));
$this->print_symbol($size - $i);
echo("\n");
}
}
}
function main() {
$obj = new MyPattern();
//Test Case
$obj->double_right_arrow(3);
$obj->double_right_arrow(6);
$obj->double_right_arrow(7);
}
main();
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// Node Js Program
// Print Double Right Arrow Pattern
class MyPattern {
/*Include Space of given size*/
space(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
process.stdout.write(" ");
}
}
/*Include Symbol*/
print_symbol(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
process.stdout.write(">");
} else {
process.stdout.write("-");
}
}
}
//Display the double right arrow of given size
double_right_arrow(size) {
process.stdout.write("\n Size : " + size + " \n");
var i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
this.space(i);
this.print_symbol(i);
this.space(size + (parseInt(size / 2)) - i);
this.print_symbol(i);
process.stdout.write("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
this.space(size - i);
this.print_symbol(size - i);
this.space(i + (parseInt(size / 2)));
this.print_symbol(size - i);
process.stdout.write("\n");
}
}
}
function main(args) {
var obj = new MyPattern();
//Test Case
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
}
main();
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
# Python 3 Program
# Print Double Right Arrow Pattern
class MyPattern :
# Include Space of given size
def space(self, size) :
counter = 0
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Include Symbol
def print_symbol(self, size) :
counter = 0
while (counter < size) :
if (counter == 0 or counter + 1 == size) :
print(">", end = "")
else :
print("-", end = "")
counter += 1
# Display the double right arrow of given size
def double_right_arrow(self, size) :
print("\n Size : ", size ," \n", end = "")
i = 0
# Display the result of top half shell
while (i < size) :
self.space(i)
self.print_symbol(i)
self.space(size + (int(size / 2)) - i)
self.print_symbol(i)
print("\n", end = "")
i += 1
# Display the result of bottom half shell
i = 0
while (i < size) :
self.space(size - i)
self.print_symbol(size - i)
self.space(i + (int(size / 2)))
self.print_symbol(size - i)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
obj.double_right_arrow(3)
obj.double_right_arrow(6)
obj.double_right_arrow(7)
if __name__ == "__main__":
main()
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
# Ruby Program
# Print Double Right Arrow Pattern
class MyPattern
# Include Space of given size
def space(size)
counter = 0
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
# Include Symbol
def print_symbol(size)
counter = 0
while (counter < size)
if (counter == 0 ||
counter + 1 == size)
print(">")
else
print("-")
end
counter += 1
end
end
# Display the double right arrow of given size
def double_right_arrow(size)
print("\n Size :", size ," \n")
i = 0
# Display the result of top half shell
while (i < size)
self.space(i)
self.print_symbol(i)
self.space(size + (size / 2) - i)
self.print_symbol(i)
print("\n")
i += 1
end
# Display the result of bottom half shell
i = 0
while (i < size)
self.space(size - i)
self.print_symbol(size - i)
self.space(i + (size / 2))
self.print_symbol(size - i)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.double_right_arrow(3)
obj.double_right_arrow(6)
obj.double_right_arrow(7)
end
main()
Output
Size :3
> >
>> >>
>-> >->
>> >>
> >
Size :6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size :7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// Scala Program
// Print Double Right Arrow Pattern
class MyPattern {
/*Include Space of given size*/
def space(size: Int): Unit = {
var counter: Int = 0;
counter = 0;
while (counter < size) {
print(" ");
counter += 1;
}
}
/*Include Symbol*/
def print_symbol(size: Int): Unit = {
var counter: Int = 0;
while (counter < size) {
if (counter == 0 ||
counter + 1 == size) {
print(">");
} else {
print("-");
}
counter += 1;
}
}
//Display the double right arrow of given size
def double_right_arrow(size: Int): Unit = {
print("\n Size : " + size + " \n");
var i: Int = 0;
//Display the result of top half shell
while (i < size) {
space(i);
print_symbol(i);
space(size + ((size / 2).toInt) - i);
print_symbol(i);
print("\n");
i += 1;
}
//Display the result of bottom half shell
i = 0;
while (i < size) {
space(size - i);
print_symbol(size - i);
space(i + ((size / 2).toInt));
print_symbol(size - i);
print("\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
}
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
// Swift Program
// Print Double Right Arrow Pattern
class MyPattern {
/*Include Space of given size*/
func space(_ size: Int) {
var counter: Int = 0;
while (counter < size) {
print(" ", terminator: "");
counter += 1;
}
}
/*Include Symbol*/
func print_symbol(_ size: Int) {
var counter: Int = 0;
while (counter < size) {
if (counter == 0 ||
counter + 1 == size) {
print(">", terminator: "");
} else {
print("-", terminator: "");
}
counter += 1;
}
}
//Display the double right arrow of given size
func double_right_arrow(_ size: Int) {
print("\n Size : ", size ," \n", terminator: "");
var i: Int = 0;
//Display the result of top half shell
while (i < size) {
self.space(i);
self.print_symbol(i);
self.space(size + (size / 2) - i);
self.print_symbol(i);
print("\n", terminator: "");
i += 1;
}
//Display the result of bottom half shell
i = 0;
while (i < size) {
self.space(size - i);
self.print_symbol(size - i);
self.space(i + (size / 2));
self.print_symbol(size - i);
print("\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj: MyPattern = MyPattern();
obj.double_right_arrow(3);
obj.double_right_arrow(6);
obj.double_right_arrow(7);
}
main();
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
fn main() {
//Test Cases
double_right_arrow(3);
double_right_arrow(6);
double_right_arrow(7);
}
fn double_right_arrow(size: i32) {
print!("\n Size : {} \n", size);
let mut i: i32 = 0;
//Display the result of top half shell
while i < size {
space(i);
print_symbol(i);
space(size + (size / 2) - i);
print_symbol(i);
print!("\n");
i += 1;
}
i = 0;
//Display the result of bottom half shell
while i < size {
space(size - i);
print_symbol(size - i);
space(i + (size / 2));
print_symbol(size - i);
print!("\n");
i += 1;
}
}
fn print_symbol(size: i32) {
let mut counter: i32 = 0;
while counter < size {
if counter == 0 || counter + 1 == size {
print!(">");
}
else {
print!("-");
}
counter += 1;
}
}
fn space(size: i32) {
let mut counter: i32 = 0;
while counter < size {
//Add space
print!(" ");
counter += 1;
}
}
Output
Size : 3
> >
>> >>
>-> >->
>> >>
> >
Size : 6
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
Size : 7
> >
>> >>
>-> >->
>--> >-->
>---> >--->
>----> >---->
>-----> >----->
>----> >---->
>---> >--->
>--> >-->
>-> >->
>> >>
> >
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