Skip to main content

Print flying machine pattern

Given an integer height, our goal is to display a relevant flying machine pattern of given this even height. For example.

Flying machine pattern

In this example outer layer is form of ☆ symbol. and inner layer is form of an ☀ symbol. Here given code implementation process.

//C Program 
//Print flying machine pattern
#include <stdio.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 flying machine pattern of given height
void flying_pattern(int height)
{
  if (height < 4 || height % 2 != 0)
  {
    return;
  }
  printf("\n Height : %d  \n", height);
  int i = 0;
  //Display the result of top half shell
  for (i = 0; i < height / 2; i++)
  {
    //include initial space
    space((height * 2) - (i * 2));
    //include symbol we can use any symbol
    print_symbol((i * 4) + 1);
    printf("\n");
  }
  //Display the result of bottom half shell
  for (i = 0; i < height / 2; i++)
  {
    //include initial space
    space(((height / 2) - i) * 2);
    print_symbol((height / 2 - i) * 2 - 1);
    space((i * 8) + 3);
    print_symbol((height / 2 - i) * 2 - 1);
    printf("\n");
  }
}
int main()
{
  //Test Cases
  flying_pattern(6);
  flying_pattern(8);
  flying_pattern(12);
  return 0;
}

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  Java Program
  Print flying machine pattern
*/
class MyPattern
{
  public void space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      System.out.print(" ");
    }
  }
  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 flying machine pattern of given height
  public void flying_pattern(int height)
  {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    System.out.print("\n Height : " + height + " \n");
    int i = 0;
    //Display the result of top half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      print_symbol((i * 4) + 1);
      System.out.print("\n");
    }
    //Display the result of bottom half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      space(((height / 2) - i) * 2);
      print_symbol((height / 2 - i) * 2 - 1);
      space((i * 8) + 3);
      print_symbol((height / 2 - i) * 2 - 1);
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.flying_pattern(6);
    obj.flying_pattern(8);
    obj.flying_pattern(12);
  }
}

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  C++ Program
  Print flying machine pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
  public: void space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      cout << " ";
    }
  }
  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 flying machine pattern of given height
  void flying_pattern(int height)
  {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    cout << "\n Height : " << height << " \n";
    int i = 0;
    //Display the result of top half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      this->space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      this->print_symbol((i * 4) + 1);
      cout << "\n";
    }
    //Display the result of bottom half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      this->space(((height / 2) - i) * 2);
      this->print_symbol((height / 2 - i) * 2 - 1);
      this->space((i * 8) + 3);
      this->print_symbol((height / 2 - i) * 2 - 1);
      cout << "\n";
    }
  }
};
int main()
{
  MyPattern obj;
  //Test Cases
  obj.flying_pattern(6);
  obj.flying_pattern(8);
  obj.flying_pattern(12);
  return 0;
}

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  C# Program
  Print flying machine pattern
*/
using System;
class MyPattern
{
  public void space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      Console.Write(" ");
    }
  }
  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 flying machine pattern of given height
  public void flying_pattern(int height)
  {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    Console.Write("\n Height : " + height + " \n");
    int i = 0;
    //Display the result of top half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      print_symbol((i * 4) + 1);
      Console.Write("\n");
    }
    //Display the result of bottom half shell
    for (i = 0; i < height / 2; i++)
    {
      //include initial space
      space(((height / 2) - i) * 2);
      print_symbol((height / 2 - i) * 2 - 1);
      space((i * 8) + 3);
      print_symbol((height / 2 - i) * 2 - 1);
      Console.Write("\n");
    }
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.flying_pattern(6);
    obj.flying_pattern(8);
    obj.flying_pattern(12);
  }
}

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
<?php
/*
  Php Program
  Print flying machine pattern
*/
class MyPattern
{
  function space($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      echo " ";
    }
  }

  function print_symbol($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      if ($counter == 0 || $counter + 1 == $size)
      {
        echo "☆";
      }
      else
      {
        echo "☀";
      }
    }
  }
  //Display the flying machine pattern of given height
  function flying_pattern($height)
  {
    if ($height < 4 || $height % 2 != 0)
    {
      return;
    }
    echo "\n Height : ". $height ." \n";
    $i = 0;
    //Display the result of top half shell
    for ($i = 0; $i < intval($height / 2); $i++)
    {
      //include initial space
      $this->space(($height * 2) - ($i * 2));
      //include symbol we can use any symbol
      $this->print_symbol(($i * 4) + 1);
      echo "\n";
    }
    //Display the result of bottom half shell
    for ($i = 0; $i < intval($height / 2); $i++)
    {
      //include initial space
      $this->space(((intval($height / 2)) - $i) * 2);
      $this->print_symbol((intval($height / 2) - $i) * 2 - 1);
      $this->space(($i * 8) + 3);
      $this->print_symbol((intval($height / 2) - $i) * 2 - 1);
      echo "\n";
    }
  }
}

function main()
{
  $obj = new MyPattern();
  //Test Cases
  $obj->flying_pattern(6);
  $obj->flying_pattern(8);
  $obj->flying_pattern(12);
}
main();

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  Node Js Program
  Print flying machine pattern
*/
class MyPattern
{
  space(size)
  {
    var counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      process.stdout.write(" ");
    }
  }
  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 flying machine pattern of given height
  flying_pattern(height)
  {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    process.stdout.write("\n Height : " + height + " \n");
    var i = 0;
    //Display the result of top half shell
    for (i = 0; i < parseInt(height / 2); i++)
    {
      //include initial space
      this.space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      this.print_symbol((i * 4) + 1);
      process.stdout.write("\n");
    }
    //Display the result of bottom half shell
    for (i = 0; i < parseInt(height / 2); i++)
    {
      //include initial space
      this.space(((parseInt(height / 2)) - i) * 2);
      this.print_symbol((parseInt(height / 2) - i) * 2 - 1);
      this.space((i * 8) + 3);
      this.print_symbol((parseInt(height / 2) - i) * 2 - 1);
      process.stdout.write("\n");
    }
  }
}

function main()
{
  var obj = new MyPattern();
  //Test Cases
  obj.flying_pattern(6);
  obj.flying_pattern(8);
  obj.flying_pattern(12);
}
main();

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
#   Python 3 Program
#   Print flying machine pattern

class MyPattern :
  def space(self, size) :
    counter = 0
    while (counter < size) :
      print(" ", end = "")
      counter += 1
    
  
  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 flying machine pattern of given height
  def flying_pattern(self, height) :
    if (height < 4 or height % 2 != 0) :
      return
    
    print("\n Height : ", height ," \n", end = "")
    i = 0
    # Display the result of top half shell
    while (i < int(height / 2)) :
      # include initial space
      self.space((height * 2) - (i * 2))
      # include symbol we can use any symbol
      self.print_symbol((i * 4) + 1)
      print("\n", end = "")
      i += 1
    
    # Display the result of bottom half shell
    i = 0
    while (i < int(height / 2)) :
      # include initial space
      self.space(((int(height / 2)) - i) * 2)
      self.print_symbol((int(height / 2) - i) * 2 - 1)
      self.space((i * 8) + 3)
      self.print_symbol((int(height / 2) - i) * 2 - 1)
      print("\n", end = "")
      i += 1
    
  

def main() :
  obj = MyPattern()
  # Test Cases
  obj.flying_pattern(6)
  obj.flying_pattern(8)
  obj.flying_pattern(12)

if __name__ == "__main__": main()

Output

 Height :  6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height :  8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height :  12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
#   Ruby Program
#   Print flying machine pattern

class MyPattern

  def space(size)
  
    counter = 0
    while (counter < size)
    
      # Add space
      print(" ")
      counter += 1
    end
  end
  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 flying machine pattern of given height
  def flying_pattern(height)
  
    if (height < 4 || height % 2 != 0)
    
      return
    end
    print("\n Height : ", height ," \n")
    i = 0
    # Display the result of top half shell
    while (i < height / 2)
    
      # include initial space
      self.space((height * 2) - (i * 2))
      # include symbol we can use any symbol
      self.print_symbol((i * 4) + 1)
      print("\n")
      i += 1
    end
    # Display the result of bottom half shell
    i = 0
    while (i < height / 2)
    
      # include initial space
      self.space(((height / 2) - i) * 2)
      self.print_symbol((height / 2 - i) * 2 - 1)
      self.space((i * 8) + 3)
      self.print_symbol((height / 2 - i) * 2 - 1)
      print("\n")
      i += 1
    end
  end
end
def main()

  obj = MyPattern.new()
  # Test Cases
  obj.flying_pattern(6)
  obj.flying_pattern(8)
  obj.flying_pattern(12)
end
main()

Output

 Height : 6 
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8 
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12 
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  Scala Program
  Print flying machine pattern
*/
class MyPattern
{
  def space(size: Int): Unit = {
    var counter: Int = 0;
    while (counter < size)
    {
      //Add space
      print(" ");
      counter += 1;
    }
  }
  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 flying machine pattern of given height
  def flying_pattern(height: Int): Unit = {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    print("\n Height : " + height + " \n");
    var i: Int = 0;
    //Display the result of top half shell
    while (i < (height / 2).toInt)
    {
      //include initial space
      space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      print_symbol((i * 4) + 1);
      print("\n");
      i += 1;
    }
    //Display the result of bottom half shell
    i = 0;
    while (i < (height / 2).toInt)
    {
      //include initial space
      space((((height / 2).toInt) - i) * 2);
      print_symbol(((height / 2).toInt - i) * 2 - 1);
      space((i * 8) + 3);
      print_symbol(((height / 2).toInt - i) * 2 - 1);
      print("\n");
      i += 1;
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Test Cases
    obj.flying_pattern(6);
    obj.flying_pattern(8);
    obj.flying_pattern(12);
  }
}

Output

 Height : 6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height : 8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height : 12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆
/*
  Swift Program
  Print flying machine pattern
*/
class MyPattern
{
  func space(_ size: Int)
  {
    var counter: Int = 0;
    while (counter < size)
    {
      print(" ", terminator: "");
      counter += 1;
    }
  }
  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 flying machine pattern of given height
  func flying_pattern(_ height: Int)
  {
    if (height < 4 || height % 2 != 0)
    {
      return;
    }
    print("\n Height : ", height ," \n", terminator: "");
    var i: Int = 0;
    //Display the result of top half shell
    while (i < height / 2)
    {
      //include initial space
      self.space((height * 2) - (i * 2));
      //include symbol we can use any symbol
      self.print_symbol((i * 4) + 1);
      print("\n", terminator: "");
      i += 1;
    }
    //Display the result of bottom half shell
    i = 0;
    while (i < height / 2)
    {
      //include initial space
      self.space(((height / 2) - i) * 2);
      self.print_symbol((height / 2 - i) * 2 - 1);
      self.space((i * 8) + 3);
      self.print_symbol((height / 2 - i) * 2 - 1);
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Test Cases
  obj.flying_pattern(6);
  obj.flying_pattern(8);
  obj.flying_pattern(12);
}
main();

Output

 Height :  6
            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

 Height :  8
                ☆
              ☆☀☀☀☆
            ☆☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆   ☆☀☀☀☀☀☆
      ☆☀☀☀☆           ☆☀☀☀☆
    ☆☀☆                   ☆☀☆
  ☆                           ☆

 Height :  12
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆




Comment

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