Posted on by Kalkicode
Code Pattern

Print flying machine pattern

In this article, we will explore a C program to print a flying machine pattern. The program will generate a visually appealing pattern using symbols. We will discuss the problem statement, provide an explanation with suitable examples, present the algorithm and pseudocode, and finally explain the output and time complexity of the code.

Flying machine pattern

Problem Statement

The task is to create a C program that prints a flying machine pattern based on the given height. The height determines the size of the pattern. The pattern consists of a symmetrical representation of a flying machine using symbols.

Example:

For a height of 6, the pattern would look like this:

            ☆
          ☆☀☀☀☆
        ☆☀☀☀☀☀☀☀☆
      ☆☀☀☀☆   ☆☀☀☀☆
    ☆☀☆           ☆☀☆
  ☆                   ☆

Algorithm and Pseudocode

1. Create a function named space that takes a size parameter. This function will print spaces based on the given size.

2. Create a function named print_symbol that takes a size parameter. This function will print symbols based on the given size. In this case, the symbol chosen is the star () for the edges and the sun () for the inner part of the machine.

3. Create a function named flying_pattern that takes a height parameter. This function will display the flying machine pattern based on the given height.

4. Inside the flying_pattern function, check if the height is less than 4 or odd. If it is, return from the function.

5. Print the height value.

6. Use a loop to display the top half shell of the pattern. Inside the loop:

  • Call the space function to print initial spaces based on the height.
  • Call the print_symbol function to print symbols based on the current iteration.
  • Print a new line.

7. Use another loop to display the bottom half shell of the pattern. Inside the loop:

  • Call the space function to print initial spaces based on the height and current iteration.
  • Call the print_symbol function to print symbols based on the current iteration.
  • Call the space function to print spaces in between the two halves.
  • Call the print_symbol function again to print symbols based on the current iteration.
  • Print a new line.

8. In the main function, test the flying_pattern function with different heights.

Output Explanation

The output shows the flying machine pattern for different heights: 6, 8, and 12. Each pattern is displayed with the height value.

The pattern is constructed using spaces and symbols. The spaces create the indentation effect, while the symbols form the shape of the flying machine. The star symbol () is used for the edges, and the sun symbol () is used for the inner part.

As the height increases, the pattern becomes more intricate, with a larger number of symbols and spaces to create the desired shape. The pattern is symmetric, and both halves are identical.

Code Solution

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

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
                        ☆
                      ☆☀☀☀☆
                    ☆☀☀☀☀☀☀☀☆
                  ☆☀☀☀☀☀☀☀☀☀☀☀☆
                ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
              ☆☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☆
            ☆☀☀☀☀☀☀☀☀☀☆   ☆☀☀☀☀☀☀☀☀☀☆
          ☆☀☀☀☀☀☀☀☆           ☆☀☀☀☀☀☀☀☆
        ☆☀☀☀☀☀☆                   ☆☀☀☀☀☀☆
      ☆☀☀☀☆                           ☆☀☀☀☆
    ☆☀☆                                   ☆☀☆
  ☆                                           ☆

Time Complexity

The time complexity of the program can be analyzed based on the number of iterations in the loops. The top half shell and the bottom half shell are printed separately, each in its respective loop.

Let n be the given height. The top half shell loop iterates n/2 times, and the bottom half shell loop also iterates n/2 times. Therefore, the total number of iterations is approximately n.

Thus, the time complexity of the code can be approximated as O(n), where n represents the height of the flying machine pattern.

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