Print the yacht shape
Problem Statement:
The task is to write a program that prints a yacht shape using asterisks (*) and dashes (-). The yacht shape consists of multiple layers, each layer being wider than the previous one until the middle, and then gradually decreasing in width.
Example:
Consider the case where the input is 5. The output will be:
* * * * * * * -* * * * - --* * * --
The above output represents the yacht shape with 5 layers. The top layer starts with a single asterisk, and each subsequent layer has one more asterisk than the previous layer. After reaching the middle layer, the width decreases by one asterisk for each subsequent layer until the bottom layer.
Algorithm:
- Define a function "includeCharacter" that takes a character and a number as parameters.
- In the "includeCharacter" function, use a loop to print the given character the specified number of times.
- Define a function "printPattern" that takes a number as a parameter.
- In the "printPattern" function, first, print the value of the input number for reference.
- Print the top layer of the yacht shape using nested loops:
- Use the outer loop to iterate from 0 to half of the input number.
- Inside the outer loop, use the "includeCharacter" function to print spaces based on the current iteration value.
- Use the inner loop to print asterisks based on the current iteration value.
- Print the bottom layer of the yacht shape using nested loops:
- Use the outer loop to iterate from 0 to half of the input number.
- Inside the outer loop, use conditional statements to determine whether to print spaces or dashes based on the current iteration value.
- Use the inner loop to print asterisks based on the current iteration value.
- Call the "printPattern" function with different input values to test the code.
Code Solution
// C program for
// Print the yacht shape
#include <stdio.h>
// This is display given text of given length
void includeCharacter(char icon, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%c", icon);
}
}
void printPattern(int n)
{
printf("\n N : %d \n\n", n);
// Print top layer
for (int i = 0; i < n / 2; ++i)
{
includeCharacter(' ', n - i);
for (int j = 0; j <= i; ++j)
{
printf("* ");
}
printf("\n");
}
// Print bottom layer
for (int i = 0; i <= n / 2; ++i)
{
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
for (int j = i; j <= n; ++j)
{
printf("* ");
}
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
printf("\n");
}
}
int main(int argc, char const *argv[])
{
// Test
printPattern(5);
printPattern(9);
printPattern(4);
printPattern(19);
return 0;
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
/*
Java Program
Print the yacht shape
*/
public class Pattern
{
// This is display given text of given length
public void includeCharacter(char icon, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(icon);
}
}
public void printPattern(int n)
{
System.out.print("\n N : " + n + " \n\n");
// Print top layer
for (int i = 0; i < n / 2; ++i)
{
includeCharacter(' ', n - i);
for (int j = 0; j <= i; ++j)
{
System.out.print("* ");
}
System.out.print("\n");
}
// Print bottom layer
for (int i = 0; i <= n / 2; ++i)
{
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
for (int j = i; j <= n; ++j)
{
System.out.print("* ");
}
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print the yacht shape
*/
class Pattern
{
public:
// This is display given text of given length
void includeCharacter(char icon, int n)
{
for (int i = 0; i < n; ++i)
{
cout << icon;
}
}
void printPattern(int n)
{
cout << "\n N : " << n << " \n\n";
// Print top layer
for (int i = 0; i < n / 2; ++i)
{
this->includeCharacter(' ', n - i);
for (int j = 0; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
// Print bottom layer
for (int i = 0; i <= n / 2; ++i)
{
if (i < n / 4)
{
this->includeCharacter(' ', i);
}
else
{
this->includeCharacter('-', i);
}
for (int j = i; j <= n; ++j)
{
cout << "* ";
}
if (i < n / 4)
{
this->includeCharacter(' ', i);
}
else
{
this->includeCharacter('-', i);
}
cout << "\n";
}
}
};
int main()
{
Pattern *task = new Pattern();
// Test
task->printPattern(5);
task->printPattern(9);
task->printPattern(4);
task->printPattern(19);
return 0;
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
// Include namespace system
using System;
/*
Csharp Program
Print the yacht shape
*/
public class Pattern
{
// This is display given text of given length
public void includeCharacter(char icon, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(icon);
}
}
public void printPattern(int n)
{
Console.Write("\n N : " + n + " \n\n");
// Print top layer
for (int i = 0; i < n / 2; ++i)
{
this.includeCharacter(' ', n - i);
for (int j = 0; j <= i; ++j)
{
Console.Write("* ");
}
Console.Write("\n");
}
// Print bottom layer
for (int i = 0; i <= n / 2; ++i)
{
if (i < n / 4)
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
for (int j = i; j <= n; ++j)
{
Console.Write("* ");
}
if (i < n / 4)
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
package main
import "fmt"
/*
Go Program
Print the yacht shape
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
// This is display given text of given length
func(this Pattern) includeCharacter(icon byte, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(string(icon))
}
}
func(this Pattern) printPattern(n int) {
fmt.Print("\n N : ", n, " \n\n")
// Print top layer
for i := 0 ; i < n / 2 ; i++ {
this.includeCharacter(' ', n - i)
for j := 0 ; j <= i ; j++ {
fmt.Print("* ")
}
fmt.Print("\n")
}
// Print bottom layer
for i := 0 ; i <= n / 2 ; i++ {
if i < n / 4 {
this.includeCharacter(' ', i)
} else {
this.includeCharacter('-', i)
}
for j := i ; j <= n ; j++ {
fmt.Print("* ")
}
if i < n / 4 {
this.includeCharacter(' ', i)
} else {
this.includeCharacter('-', i)
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
task.printPattern(5)
task.printPattern(9)
task.printPattern(4)
task.printPattern(19)
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
<?php
/*
Php Program
Print the yacht shape
*/
class Pattern
{
// This is display given text of given length
public function includeCharacter($icon, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo($icon);
}
}
public function printPattern($n)
{
echo("\n N : ".$n." \n\n");
// Print top layer
for ($i = 0; $i < (int)($n / 2); ++$i)
{
$this->includeCharacter(' ', $n - $i);
for ($j = 0; $j <= $i; ++$j)
{
echo("* ");
}
echo("\n");
}
// Print bottom layer
for ($i = 0; $i <= (int)($n / 2); ++$i)
{
if ($i < (int)($n / 4))
{
$this->includeCharacter(' ', $i);
}
else
{
$this->includeCharacter('-', $i);
}
for ($j = $i; $j <= $n; ++$j)
{
echo("* ");
}
if ($i < (int)($n / 4))
{
$this->includeCharacter(' ', $i);
}
else
{
$this->includeCharacter('-', $i);
}
echo("\n");
}
}
}
function main()
{
$task = new Pattern();
// Test
$task->printPattern(5);
$task->printPattern(9);
$task->printPattern(4);
$task->printPattern(19);
}
main();
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
/*
Node JS Program
Print the yacht shape
*/
class Pattern
{
// This is display given text of given length
includeCharacter(icon, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(icon);
}
}
printPattern(n)
{
process.stdout.write("\n N : " + n + " \n\n");
// Print top layer
for (var i = 0; i < parseInt(n / 2); ++i)
{
this.includeCharacter(' ', n - i);
for (var j = 0; j <= i; ++j)
{
process.stdout.write("* ");
}
process.stdout.write("\n");
}
// Print bottom layer
for (var i = 0; i <= parseInt(n / 2); ++i)
{
if (i < parseInt(n / 4))
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
for (var j = i; j <= n; ++j)
{
process.stdout.write("* ");
}
if (i < parseInt(n / 4))
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
process.stdout.write("\n");
}
}
}
function main()
{
var task = new Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
main();
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
# Python 3 Program
# Print the yacht shape
class Pattern :
# This is display given text of given length
def includeCharacter(self, icon, n) :
i = 0
while (i < n) :
print(icon, end = "")
i += 1
def printPattern(self, n) :
print("\n N : ", n ," \n")
i = 0
# Print top layer
while (i < int(n / 2)) :
self.includeCharacter(' ', n - i)
j = 0
while (j <= i) :
print("* ", end = "")
j += 1
print(end = "\n")
i += 1
i = 0
# Print bottom layer
while (i <= int(n / 2)) :
if (i < int(n / 4)) :
self.includeCharacter(' ', i)
else :
self.includeCharacter('-', i)
j = i
while (j <= n) :
print("* ", end = "")
j += 1
if (i < int(n / 4)) :
self.includeCharacter(' ', i)
else :
self.includeCharacter('-', i)
print(end = "\n")
i += 1
def main() :
task = Pattern()
# Test
task.printPattern(5)
task.printPattern(9)
task.printPattern(4)
task.printPattern(19)
if __name__ == "__main__": main()
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
# Ruby Program
# Print the yacht shape
class Pattern
# This is display given text of given length
def includeCharacter(icon, n)
i = 0
while (i < n)
print(icon)
i += 1
end
end
def printPattern(n)
print("\n N : ", n ," \n\n")
i = 0
# Print top layer
while (i < n / 2)
self.includeCharacter(' ', n - i)
j = 0
while (j <= i)
print("* ")
j += 1
end
print("\n")
i += 1
end
i = 0
# Print bottom layer
while (i <= n / 2)
if (i < n / 4)
self.includeCharacter(' ', i)
else
self.includeCharacter('-', i)
end
j = i
while (j <= n)
print("* ")
j += 1
end
if (i < n / 4)
self.includeCharacter(' ', i)
else
self.includeCharacter('-', i)
end
print("\n")
i += 1
end
end
end
def main()
task = Pattern.new()
# Test
task.printPattern(5)
task.printPattern(9)
task.printPattern(4)
task.printPattern(19)
end
main()
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
/*
Scala Program
Print the yacht shape
*/
class Pattern()
{
// This is display given text of given length
def includeCharacter(icon: Char, n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(icon);
i += 1;
}
}
def printPattern(n: Int): Unit = {
print("\n N : " + n + " \n\n");
var i: Int = 0;
// Print top layer
while (i < n / 2)
{
includeCharacter(' ', n - i);
var j: Int = 0;
while (j <= i)
{
print("* ");
j += 1;
}
print("\n");
i += 1;
}
i = 0;
// Print bottom layer
while (i <= n / 2)
{
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
var j: Int = i;
while (j <= n)
{
print("* ");
j += 1;
}
if (i < n / 4)
{
includeCharacter(' ', i);
}
else
{
includeCharacter('-', i);
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
/*
Swift 4 Program
Print the yacht shape
*/
class Pattern
{
// This is display given text of given length
func includeCharacter(_ icon: Character, _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(icon, terminator: "");
i += 1;
}
}
func printPattern(_ n: Int)
{
print("\n N : ", n ," \n");
var i: Int = 0;
// Print top layer
while (i < n / 2)
{
self.includeCharacter(" ", n - i);
var j: Int = 0;
while (j <= i)
{
print("* ", terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
}
i = 0;
// Print bottom layer
while (i <= n / 2)
{
if (i < n / 4)
{
self.includeCharacter(" ", i);
}
else
{
self.includeCharacter("-", i);
}
var j: Int = i;
while (j <= n)
{
print("* ", terminator: "");
j += 1;
}
if (i < n / 4)
{
self.includeCharacter(" ", i);
}
else
{
self.includeCharacter("-", i);
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
let task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
main();
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
/*
Kotlin Program
Print the yacht shape
*/
class Pattern
{
// This is display given text of given length
fun includeCharacter(icon: Char, n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(icon);
i += 1;
}
}
fun printPattern(n: Int): Unit
{
print("\n N : " + n + " \n\n");
var i: Int = 0;
// Print top layer
while (i < n / 2)
{
this.includeCharacter(' ', n - i);
var j: Int = 0;
while (j <= i)
{
print("* ");
j += 1;
}
print("\n");
i += 1;
}
i = 0;
// Print bottom layer
while (i <= n / 2)
{
if (i < n / 4)
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
var j: Int = i;
while (j <= n)
{
print("* ");
j += 1;
}
if (i < n / 4)
{
this.includeCharacter(' ', i);
}
else
{
this.includeCharacter('-', i);
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Pattern = Pattern();
// Test
task.printPattern(5);
task.printPattern(9);
task.printPattern(4);
task.printPattern(19);
}
Output
N : 5
*
* *
* * * * * *
-* * * * * -
--* * * * --
N : 9
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
--* * * * * * * * --
---* * * * * * * ---
----* * * * * * ----
N : 4
*
* *
* * * * *
-* * * * -
--* * * --
N : 19
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
----* * * * * * * * * * * * * * * * ----
-----* * * * * * * * * * * * * * * -----
------* * * * * * * * * * * * * * ------
-------* * * * * * * * * * * * * -------
--------* * * * * * * * * * * * --------
---------* * * * * * * * * * * ---------
Time Complexity:
The time complexity of the code is O(n^2), where n is the input number. This is because there are nested loops that iterate up to n/2 times, resulting in a quadratic time complexity.
Resultant Output Explanation:
The resultant output is the yacht shape printed using asterisks (*) and dashes (-). Each layer is created by printing the respective characters in the required pattern. The top layer starts with a single asterisk and expands by one asterisk in each subsequent row. The bottom layer starts with spaces and gradually transitions to dashes while printing the asterisks in the same pattern as the top layer.
For example, when the input is 5, the top layer has a width of 5 asterisks in the middle row. The subsequent rows have decreasing width until the top layer is completed. Then, the bottom layer starts with spaces and gradually transitions to dashes, while maintaining the same pattern of asterisks as the top layer. The bottom layer also has decreasing width until the bottom row is reached.
The output visually represents the yacht shape, with the top and bottom layers symmetrically aligned.
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