Skip to main content

Print the octagonal pattern

The octagonal pattern is a geometric pattern consisting of octagon shapes arranged in a specific manner. In this article, we will discuss how to generate the octagonal pattern using C programming. The octagonal pattern can be formed by printing a series of stars (*) and spaces in a specific order.

Problem Statement:

Write a program to print the octagonal pattern of a given size. The program should take an integer as input representing the size of the octagon's side, and it should output the octagonal pattern accordingly.

Explanation with an Example:

Let's consider an example to understand the problem and its solution. Suppose we want to print the octagonal pattern for a size of 4. The expected output will be as follows:

      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
   * * * * * * *
   * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
  

Algorithm:

To solve this problem, we can follow these steps:

  1. Define a function to print spaces. This function will take an integer as input representing the number of spaces to be printed. Inside the function, use a loop to print the required number of spaces.
  2. Define a function to print symbols (stars '*'). This function will take an integer as input representing the number of symbols to be printed. Inside the function, use a loop to print the required number of symbols.
  3. Define the main function. Inside the main function, we will call the other two functions to print the octagonal pattern.
  4. Within the main function, iterate from 0 to size-1 and do the following:
    • Print spaces(size-i) using the space function.
    • Print symbols(size+i) using the print_symbol function.
    • Print a new line.
  5. Next, iterate from 0 to size-2 and do the following:
    • Print a space.
    • Print symbols((size)*2-1) using the print_symbol function.
    • Print a new line.
  6. Finally, iterate from 0 to size-2 and do the following:
    • Print spaces(i+2) using the space function.
    • Print symbols((size-1)*2-i) using the print_symbol function.
    • Print a new line.

Pseudocode:

function space(size):
    for counter from 0 to size-1:
        print a space

function print_symbol(size):
    for counter from 0 to size-1:
        print a symbol '* '

function print_octagonal(size):
    if size < 0:
        return
    print "Side: size"
    for i from 0 to size-1:
        space(size-i)
        print_symbol(size+i)
        print a new line
    for i from 0 to size-2:
        print a space
        print_symbol((size)*2-1)
        print a new line
    for i from 0 to size-2:
        space(i+2)
        print_symbol((size-1)*2-i)
        print a new line

function main():
    print_octagonal(4)
    print_octagonal(5)
    print_octagonal(7)

Code Solution

//C Program 
//Print the octagonal 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(" ");
  }
}

void print_symbol(int size) {
  int counter = 0;
  for (counter = 0; counter < size; counter++) {
    printf("* ");
  }
}
//Display octagonal pattern of given side 
void print_octagonal(int size) {
  if(size<0 )
  {
    return;
  }
  
  printf("\n  Side : %d \n\n", size);
  
  int j=0;
  int i = 0;

  for (i = 0; i < size; ++i)
  {  
    //Print top section
    space(size-i);
    print_symbol(size+i);
    printf("\n");
  }
  for (i = 0; i < size-1; ++i)
  {  
    //Display middle layer
    printf(" ");
    print_symbol((size)*2-1);
    printf("\n");
  }
  for (i = 0; i < size-1; ++i)
  {  
    //Display bottom layer
    space(i+2);
    print_symbol((size-1)*2-i);
    printf("\n");
  }
}

int main() {
  //Test Cases
  print_octagonal(4);
  print_octagonal(5);
  print_octagonal(7);
  return 0;
}

Output

  Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

  Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

  Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
/*
  Java Program
  Print the octagonal 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++)
    {
      System.out.print("* ");
    }
  }
  //Display octagonal pattern of given side 
  public void print_octagonal(int size)
  {
    if (size < 0)
    {
      return;
    }
    System.out.print("\n Side : " + size + " \n\n");
    int j = 0;
    int i = 0;
    for (i = 0; i < size; ++i)
    {
      //Print top section
      space(size - i);
      print_symbol(size + i);
      System.out.print("\n");
    }
    for (i = 0; i < size - 1; ++i)
    {
      //Display middle layer
      System.out.print(" ");
      print_symbol((size) * 2 - 1);
      System.out.print("\n");
    }
    for (i = 0; i < size - 1; ++i)
    {
      //Display bottom layer
      space(i + 2);
      print_symbol((size - 1) * 2 - i);
      System.out.print("\n");
    }
  }
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.print_octagonal(4);
    obj.print_octagonal(5);
    obj.print_octagonal(7);
  }
}

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
/*
  C++ Program
  Print the octagonal 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++)
    {
      cout << "* ";
    }
  }
  //Display octagonal pattern of given side 
  void print_octagonal(int size)
  {
    if (size < 0)
    {
      return;
    }
    cout << "\n Side : " << size << " \n\n";
    int j = 0;
    int i = 0;
    for (i = 0; i < size; ++i)
    {
      //Print top section
      this->space(size - i);
      this->print_symbol(size + i);
      cout << "\n";
    }
    for (i = 0; i < size - 1; ++i)
    {
      cout << " ";
      this->print_symbol((size) * 2 - 1);
      cout << "\n";
    }
    for (i = 0; i < size - 1; ++i)
    {
      //Display bottom layer
      this->space(i + 2);
      this->print_symbol((size - 1) * 2 - i);
      cout << "\n";
    }
  }
};
int main()
{
  MyPattern obj =  MyPattern();
  //Test Cases
  obj.print_octagonal(4);
  obj.print_octagonal(5);
  obj.print_octagonal(7);
  return 0;
}

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
/*
  C# Program
  Print the octagonal pattern
*/
using System;
class MyPattern
{
  public void space(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      Console.Write(" ");
    }
  }
  public void print_symbol(int size)
  {
    int counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      Console.Write("* ");
    }
  }
  //Display octagonal pattern of given side 
  public void print_octagonal(int size)
  {
    if (size < 0)
    {
      return;
    }
    Console.Write("\n Side : " + size + " \n\n");
    int i = 0;
    for (i = 0; i < size; i++)
    {
      //Print top section
      space(size - i);
      print_symbol(size + i);
      Console.Write("\n");
    }
    for (i = 0; i < size - 1; i++)
    {
      //Display middle layer
      Console.Write(" ");
      print_symbol((size) * 2 - 1);
      Console.Write("\n");
    }
    for (i = 0; i < size - 1; i++)
    {
      //Display bottom layer
      space(i + 2);
      print_symbol((size - 1) * 2 - i);
      Console.Write("\n");
    }
  }
  public static void Main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.print_octagonal(4);
    obj.print_octagonal(5);
    obj.print_octagonal(7);
  }
}

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
<?php
/*
  Php Program
  Print the octagonal pattern
*/
class MyPattern
{
  public  function space($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      //Add space
      echo(" ");
    }
  }
  public  function print_symbol($size)
  {
    $counter = 0;
    for ($counter = 0; $counter < $size; $counter++)
    {
      echo("* ");
    }
  }
  //Display octagonal pattern of given side 
  public  function print_octagonal($size)
  {
    if ($size < 0)
    {
      return;
    }
    echo("\n Side : ". $size ." \n\n");
    $i = 0;
    for ($i = 0; $i < $size; ++$i)
    {
      //Print top section
      $this->space($size - $i);
      $this->print_symbol($size + $i);
      echo("\n");
    }
    for ($i = 0; $i < $size - 1; ++$i)
    {
      //Display middle layer
      echo(" ");
      $this->print_symbol(($size) * 2 - 1);
      echo("\n");
    }
    for ($i = 0; $i < $size - 1; ++$i)
    {
      //Display bottom layer
      $this->space($i + 2);
      $this->print_symbol(($size - 1) * 2 - $i);
      echo("\n");
    }
  }
}

function main()
{
  $obj = new MyPattern();
  //Test Cases
  $obj->print_octagonal(4);
  $obj->print_octagonal(5);
  $obj->print_octagonal(7);
}
main();

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
/*
  Node Js Program
  Print the octagonal pattern
*/
class MyPattern
{
  space(size)
  {
    var counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      //Add space
      process.stdout.write(" ");
    }
  }
  print_symbol(size)
  {
    var counter = 0;
    for (counter = 0; counter < size; counter++)
    {
      process.stdout.write("* ");
    }
  }
  //Display octagonal pattern of given side 
  print_octagonal(size)
  {
    if (size < 0)
    {
      return;
    }
    process.stdout.write("\n Side : " + size + " \n\n");
    var i = 0;
    for (i = 0; i < size; ++i)
    {
      //Print top section
      this.space(size - i);
      this.print_symbol(size + i);
      process.stdout.write("\n");
    }
    for (i = 0; i < size - 1; ++i)
    {
      //Display middle layer
      process.stdout.write(" ");
      this.print_symbol((size) * 2 - 1);
      process.stdout.write("\n");
    }
    for (i = 0; i < size - 1; ++i)
    {
      //Display bottom layer
      this.space(i + 2);
      this.print_symbol((size - 1) * 2 - i);
      process.stdout.write("\n");
    }
  }
}

function main(args)
{
  var obj = new MyPattern();
  //Test Cases
  obj.print_octagonal(4);
  obj.print_octagonal(5);
  obj.print_octagonal(7);
}
main();

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
#   Python 3 Program
#   Print the octagonal pattern

class MyPattern :
  def space(self, size) :
    counter = 0
    counter = 0
    while (counter < size) :
      print(" ", end = "")
      counter += 1
    
  
  def print_symbol(self, size) :
    counter = 0
    counter = 0
    while (counter < size) :
      print("* ", end = "")
      counter += 1
    
  
  # Display octagonal pattern of given side 
  def print_octagonal(self, size) :
    if (size < 0) :
      return
    
    print("\n Side : ", size ," \n\n", end = "")
    i = 0
    while (i < size) :
      # Print top section
      self.space(size - i)
      self.print_symbol(size + i)
      print("\n", end = "")
      i += 1
    
    i = 0
    while (i < size - 1) :
      # Display middle layer
      print(" ", end = "")
      self.print_symbol((size) * 2 - 1)
      print("\n", end = "")
      i += 1
    
    i = 0
    while (i < size - 1) :
      # Display bottom layer
      self.space(i + 2)
      self.print_symbol((size - 1) * 2 - i)
      print("\n", end = "")
      i += 1
    
  

def main() :
  obj = MyPattern()
  # Test Cases
  obj.print_octagonal(4)
  obj.print_octagonal(5)
  obj.print_octagonal(7)


if __name__ == "__main__": main()

Output

 Side :  4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side :  5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side :  7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
#   Ruby Program
#   Print the octagonal pattern

class MyPattern

  def space(size)
  
    counter = 0
    counter = 0
    while (counter < size)
    
      print(" ")
      counter += 1
    end
  end
  def print_symbol(size)
  
    counter = 0
    counter = 0
    while (counter < size)
    
      print("* ")
      counter += 1
    end
  end
  # Display octagonal pattern of given side 
  def print_octagonal(size)
  
    if (size < 0)
    
      return
    end
    print("\n Side : ", size ," \n\n")
    i = 0
    while (i < size)
    
      # Print top section
      self.space(size - i)
      self.print_symbol(size + i)
      print("\n")
      i += 1
    end
    i = 0
    while (i < size - 1)
    
      # Display middle layer
      print(" ")
      self.print_symbol((size) * 2 - 1)
      print("\n")
      i += 1
    end
    i = 0
    while (i < size - 1)
    
      # Display bottom layer
      self.space(i + 2)
      self.print_symbol((size - 1) * 2 - i)
      print("\n")
      i += 1
    end
  end
end
def main()

  obj = MyPattern.new()
  # Test Cases
  obj.print_octagonal(4)
  obj.print_octagonal(5)
  obj.print_octagonal(7)
end
main()

Output

 Side : 4 

    * * * * 
   * * * * * 
  * * * * * * 
 * * * * * * * 
 * * * * * * * 
 * * * * * * * 
 * * * * * * * 
  * * * * * * 
   * * * * * 
    * * * * 

 Side : 5 

     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 

 Side : 7 

       * * * * * * * 
      * * * * * * * * 
     * * * * * * * * * 
    * * * * * * * * * * 
   * * * * * * * * * * * 
  * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
 * * * * * * * * * * * * * 
  * * * * * * * * * * * * 
   * * * * * * * * * * * 
    * * * * * * * * * * 
     * * * * * * * * * 
      * * * * * * * * 
       * * * * * * * 
/*
  Scala Program
  Print the octagonal pattern
*/
class MyPattern
{
  def space(size: Int): Unit = {
    var counter: Int = 0;
    counter = 0;
    while (counter < size)
    {
      print(" ");
      counter += 1;
    }
  }
  def print_symbol(size: Int): Unit = {
    var counter: Int = 0;
    counter = 0;
    while (counter < size)
    {
      print("* ");
      counter += 1;
    }
  }
  //Display octagonal pattern of given side 
  def print_octagonal(size: Int): Unit = {
    if (size < 0)
    {
      return;
    }
    print("\n Side : " + size + " \n\n");
    var i: Int = 0;
    while (i < size)
    {
      //Print top section
      space(size - i);
      print_symbol(size + i);
      print("\n");
      i += 1;
    }
    i = 0;
    while (i < size - 1)
    {
      //Display middle layer
      print(" ");
      print_symbol((size) * 2 - 1);
      print("\n");
      i += 1;
    }
    i = 0;
    while (i < size - 1)
    {
      //Display bottom layer
      space(i + 2);
      print_symbol((size - 1) * 2 - i);
      print("\n");
      i += 1;
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: MyPattern = new MyPattern();
    //Test Cases
    obj.print_octagonal(4);
    obj.print_octagonal(5);
    obj.print_octagonal(7);
  }
}

Output

 Side : 4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side : 5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side : 7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *
/*
  Swift Program
  Print the octagonal pattern
*/
class MyPattern
{
  func space(_ size: Int)
  {
    var counter: Int = 0;
    counter = 0;
    while (counter < size)
    {
      print(" ", terminator: "");
      counter += 1;
    }
  }
  func print_symbol(_ size: Int)
  {
    var counter: Int = 0;
    counter = 0;
    while (counter < size)
    {
      print("* ", terminator: "");
      counter += 1;
    }
  }
  //Display octagonal pattern of given side 
  func print_octagonal(_ size: Int)
  {
    if (size < 0)
    {
      return;
    }
    print("\n Side : ", size ," \n\n", terminator: "");
    var i: Int = 0;
    while (i < size)
    {
      //Print top section
      self.space(size - i);
      self.print_symbol(size + i);
      print("\n", terminator: "");
      i += 1;
    }
    i = 0;
    while (i < size - 1)
    {
      //Display middle layer
      print(" ", terminator: "");
      self.print_symbol((size) * 2 - 1);
      print("\n", terminator: "");
      i += 1;
    }
    i = 0;
    while (i < size - 1)
    {
      //Display bottom layer
      self.space(i + 2);
      self.print_symbol((size - 1) * 2 - i);
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main()
{
  let obj: MyPattern = MyPattern();
  //Test Cases
  obj.print_octagonal(4);
  obj.print_octagonal(5);
  obj.print_octagonal(7);
}
main();

Output

 Side :  4

    * * * *
   * * * * *
  * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
 * * * * * * *
  * * * * * *
   * * * * *
    * * * *

 Side :  5

     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *

 Side :  7

       * * * * * * *
      * * * * * * * *
     * * * * * * * * *
    * * * * * * * * * *
   * * * * * * * * * * *
  * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
 * * * * * * * * * * * * *
  * * * * * * * * * * * *
   * * * * * * * * * * *
    * * * * * * * * * *
     * * * * * * * * *
      * * * * * * * *
       * * * * * * *

Time Complexity Analysis:

The time complexity of the program is primarily determined by the size of the octagon, denoted by the variable 'size'.

  • The space function and the print_symbol function both have a time complexity of O(size) as they involve a loop that runs 'size' number of times.
  • The print_octagonal function consists of three loops: one runs 'size' times, and the other two run 'size-2' times. Therefore, the time complexity of the print_octagonal function is O(size).
  • The main function calls the print_octagonal function three times, so the time complexity of the main function is also O(size).

Overall, the time complexity of the program is O(size).

Resultant Output Explanation:

The program prints the octagonal pattern for three different sizes: 4, 5, and 7.

The output is displayed in the form of an octagon made up of asterisks (*), with each line representing a row of the octagon. The spaces are used to align the asterisks correctly to form the octagonal shape.

The first row starts with four spaces and then prints four asterisks, followed by a newline character.

The next rows follow a similar pattern, increasing the number of asterisks in each row until the middle layer is reached. In the middle layer, a single space is printed, followed by a row with (size*2-1) asterisks.

After the middle layer, the rows start decreasing in the number of asterisks until the last row, which has the same number of asterisks as the first row.

The output for each size is separated by a blank line.





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