Posted on by Kalkicode
Code Pattern

Display mirror image of swastika pattern

The Swastika symbol is an ancient religious symbol that has been used for thousands of years in various cultures. It is considered a sacred symbol in Hinduism, Buddhism, and Jainism. However, due to its misuse by the Nazi party during World War II, it has become a controversial symbol associated with hate and racism.

Mirror image of swastika pattern

The purpose of this program is to display a mirror image of a Swastika pattern using asterisks (*) in a console window. The pattern is created by printing asterisks in specific positions to form the shape of a Swastika. The size of the pattern is specified as input to the program.

Example:

Suppose we want to display a Swastika pattern of size 5. The expected output would be as follows:

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

The output shows a Swastika pattern created using asterisks. The pattern is symmetrical, and the mirror image is displayed by reversing the pattern along the vertical axis.

Algorithm:

1. Start by defining a function named swastika_pattern that takes the size of the pattern as a parameter.

2. Check if the size is less than or equal to 3 or if it is an even number. If either of these conditions is true, return from the function, as these sizes would result in an invalid pattern.

3. Print the size of the pattern for reference.

4. Use two nested for loops to iterate over each row and column of the pattern.

5. Inside the nested loops, use conditional statements to determine when to print an asterisk (*) and when to print a space.

6. The conditions for printing an asterisk (*) are as follows:

  • If the current row is the first row (i == 0) and the current column is less than half of the size (j < size / 2), or
  • If the current row is the middle row (i == size / 2), or
  • If the current row is in the first half of the pattern (i < size / 2) and the current column is the last column (j == size - 1), or
  • If the current row is in the second half of the pattern (i > size / 2) and the current column is the first column (j == 0), or
  • If the current column is the middle column (j == size / 2), or
  • If the current row is the last row (i == size - 1) and the current column is greater than the middle column (j > size / 2).

7. If any of the above conditions are true, print an asterisk (*); otherwise, print a space.

8. After printing all the characters in a row, move to the next line.

9. Outside the function, in the main function, call swastika_pattern with different sizes to test the program.

Pseudocode:


function swastika_pattern(size):
    if size <= 3 or size is even:
        return
    print "Size: " + size
    for i = 0 to size-1:
        for j = 0 to size-1:
            if (i == 0 and j < size/2) or (i == size/2) or (i < size/2 and j == size-1)
               or (i > size/2 and j == 0) or (j == size/2) or (i == size-1 and j > size/2):
                print " *"
            else:
                print "  "
        print newline

main:
    swastika_pattern(5)
    swastika_pattern(7)
    swastika_pattern(9)
    swastika_pattern(11)

Code Solution

//C Program 
//Draw mirror image of swastika pattern
#include <stdio.h>

#include <stdlib.h>

//Displaying mirror image of swastika pattern in given size
void swastika_pattern(int size) {

  if(size<=3 || size%2==0)
  {
    //When get a invalid size of pattern
    return;
  }
  printf("\n Size : %d\n\n",size);
  for (int i = 0; i < size; ++i) {

    for (int j = 0; j < size; ++j) {
      //Simple test the boundary cases
      if ((i == 0 && j < size / 2) 
        || (i == (size / 2)) 
        || (i < size / 2 && j == size - 1) 
        || (i > size / 2 && j == 0) 
        || (j == (size) / 2) 
        || (i == size - 1 && j > (size) / 2)
      ) {
        printf(" *");
      } else {
        printf("  ");
      }
    }

    //include new line
    printf("\n");
  }
}
int main() {
  //Test Cases
  swastika_pattern(5);
  swastika_pattern(7);
  swastika_pattern(9);
  swastika_pattern(11);
  return 0;
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
/*
  C++ Program
  Display Wave Patterns
*/
#include<iostream>

using namespace std;
class MyPattern {
  public:

    //Displaying mirror image of swastika pattern in given size
    void swastika_pattern(int size) {
      if (size <= 3 || size % 2 == 0) {
        //When get a invalid size of pattern

        return;
      }
      cout << "\n Size : " << size << "\n\n";
      for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
          //Simple test the boundary cases

          if ((i == 0 && j < size / 2) 
              || (i == (size / 2)) 
              || (i < size / 2 && j == size - 1) 
              || (i > size / 2 && j == 0) 
              || (j == (size) / 2) 
              || (i == size - 1 && j > (size) / 2)) {
            cout << " *";
          } else {
            cout << "  ";
          }
        }
        //include new line

        cout << "\n";
      }
    }
};
int main() {
  MyPattern obj =  MyPattern();
  //Test Cases
  obj.swastika_pattern(5);
  obj.swastika_pattern(7);
  obj.swastika_pattern(9);
  obj.swastika_pattern(11);
  return 0;
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
/*
  Java Program
  Display Wave Patterns
*/

public class MyPattern {
 

  //Displaying mirror image of swastika pattern in given size
  public void swastika_pattern(int size) {

    if(size<=3 || size%2==0)
    {
      //When get a invalid size of pattern
      return;
    }
    System.out.print("\n Size : "+size+"\n\n");
    for (int i = 0; i < size; ++i) {

      for (int j = 0; j < size; ++j) {
        //Simple test the boundary cases
        if ((i == 0 && j < size / 2) 
          || (i == (size / 2)) 
          || (i < size / 2 && j == size - 1) 
          || (i > size / 2 && j == 0) 
          || (j == (size) / 2) 
          || (i == size - 1 && j > (size) / 2)
        ) {
          System.out.print(" *");
        } else {
          //include 2 space
          System.out.print("  ");
        }
      }

      //include new line
      System.out.print("\n");
    }
  }
  public static void main(String[] args) {

    MyPattern obj = new MyPattern();

    //Test Cases
    
    obj.swastika_pattern(5);
    obj.swastika_pattern(7);
    obj.swastika_pattern(9);
    obj.swastika_pattern(11);
  }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
/*
  C# Program
  Display Wave Patterns
*/
using System;

public class MyPattern {
  //Displaying mirror image of swastika pattern in given size
  public void swastika_pattern(int size) {
    if (size <= 3 || size % 2 == 0) {
      return;
    }
    Console.Write("\n Size : " + size + "\n\n");
    for (int i = 0; i < size; ++i) {
      for (int j = 0; j < size; ++j) {
        //Simple test the boundary cases

        if ((i == 0 && j < size / 2) 
                    || (i == (size / 2)) 
                    || (i < size / 2 && j == size - 1) 
                    || (i > size / 2 && j == 0) 
                    || (j == (size) / 2) 
                    || (i == size - 1 && j > (size) / 2)) {
          Console.Write(" *");
        } else {
          Console.Write("  ");
        }
      }
      Console.Write("\n");
    }
  }
  public static void Main(String[] args) {
    MyPattern obj = new MyPattern();
    obj.swastika_pattern(5);
    obj.swastika_pattern(7);
    obj.swastika_pattern(9);
    obj.swastika_pattern(11);
  }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
<?php
/*
  Php Program
  Display Wave Patterns
*/
class MyPattern {
  //Displaying mirror image of swastika pattern in given size

  public  function swastika_pattern($size) {
    if ($size <= 3 || $size % 2 == 0) {
      return;
    }
    echo("\n Size : ". $size ."\n\n");
    for ($i = 0; $i < $size; ++$i) {
      for ($j = 0; $j < $size; ++$j) {
        //Simple test the boundary cases

        if (($i == 0 && $j < intval($size / 2)) 
                    || ($i == (intval($size / 2))) 
                    || ($i < intval($size / 2) && $j == $size - 1) 
                    || ($i > intval($size / 2) && $j == 0) 
                    || ($j == intval(($size) / 2)) 
                    || ($i == $size - 1 && $j > intval(($size) / 2))) {
          echo(" *");
        } else {
          //include 2 space

          echo("  ");
        }
      }
      //include new line

      echo("\n");
    }
  }
}

function main() {
  $obj = new MyPattern();
  //Test Cases

  $obj->swastika_pattern(5);
  $obj->swastika_pattern(7);
  $obj->swastika_pattern(9);
  $obj->swastika_pattern(11);

}
main();

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
/*
  Node Js Program
  Display Wave Patterns
*/
class MyPattern {
  //Displaying mirror image of swastika pattern in given size
  swastika_pattern(size) {
    if (size <= 3 ||
      size % 2 == 0) {
      return;
    }

    process.stdout.write("\n Size : " + size + "\n\n");
    for (var i = 0; i < size; ++i) {
      for (var j = 0; j < size; ++j) {
        //Simple test the boundary cases
        if ((i == 0 &&
            j < parseInt(size / 2)) ||
          (i == (parseInt(size / 2))) ||
          (i < parseInt(size / 2) &&
            j == size - 1) ||
          (i > parseInt(size / 2) &&
            j == 0) ||
          (j == parseInt((size) / 2)) ||
          (i == size - 1 &&
            j > parseInt((size) / 2))) {
          process.stdout.write(" *");
        } else {
          //include 2 space

          process.stdout.write("  ");
        }
      }

      //include new line

      process.stdout.write("\n");
    }
  }
}

function main(args) {
  var obj = new MyPattern();
  //Test Cases
  obj.swastika_pattern(5);
  obj.swastika_pattern(7);
  obj.swastika_pattern(9);
  obj.swastika_pattern(11);
}

main();

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
# 
#   Python 3 Program
#   Display Wave Patterns

class MyPattern :
  # Displaying mirror image of swastika pattern in given size
  def swastika_pattern(self, size) :
    if (size <= 3 or size % 2 == 0) :
      return
    
    print("\n Size : ", size ,"\n\n", end = "")
    i = 0
    j = 0
    while (i < size) :
      j = 0
      while (j < size) :
        # Simple test the boundary cases
        if ((i == 0 and j < int(size / 2)) or
                (i == (int(size / 2))) or
                (i < int(size / 2) and j == size - 1) or
                (i > int(size / 2) and j == 0) or
                (j == int((size) / 2)) or
                (i == size - 1 and j > int((size) / 2))) :
          print(" *", end = "")
        else :
          print("  ", end = "")
        
        j += 1
      
      print(end = "\n")
      i += 1
    
  

def main() :
  obj = MyPattern()
  obj.swastika_pattern(5)
  obj.swastika_pattern(7)
  obj.swastika_pattern(9)
  obj.swastika_pattern(11)


if __name__ == "__main__":
  main()

Output

 Size :  5

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

 Size :  7

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

 Size :  9

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

 Size :  11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
#   Ruby Program
#   Display Wave Patterns

class MyPattern 
   # Displaying mirror image of swastika pattern in given size
  def swastika_pattern(size) 
    if (size <= 3 ||
      size % 2 == 0) 
      return
    end
    print("\n Size  : ", size ,"\n\n")
    i = 0
    j = 0
    while (i < size) 
      j = 0
      while (j < size) 
         # Simple test the boundary cases

        if ((i == 0 &&
            j < size / 2) ||
          (i == (size / 2)) ||
          (i < size / 2 &&
            j == size - 1) ||
          (i > size / 2 &&
            j == 0) ||
          (j == (size) / 2) ||
          (i == size - 1 &&
            j > (size) / 2)) 
          print(" *")
        else 
           # include two space

          print("  ")
        end
        j += 1
      end
      print("\n")
      i += 1
    end
  end
end
def main() 
  obj = MyPattern.new()
  obj.swastika_pattern(5)
  obj.swastika_pattern(7)
  obj.swastika_pattern(9)
  obj.swastika_pattern(11)
end
main()

Output

 Size  : 5

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

 Size  : 7

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

 Size  : 9

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

 Size  : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *          
 *         *          
 *         *          
 *         *          
 *         * * * * * *
/*
  Scala Program
  Display Wave Patterns
*/
class MyPattern {
  //Displaying mirror image of swastika pattern in given size
  def swastika_pattern(size: Int): Unit = {
    if (size <= 3 ||
      size % 2 == 0) {
      return;
    }
    print("\n Size : " + size + "\n\n");
    var i: Int = 0;
    var j: Int = 0;
    while (i < size) {
      j = 0;
      while (j < size) {
        //Simple test the boundary cases

        if ((i == 0 &&
            j < (size / 2).toInt) ||
          (i == ((size / 2).toInt)) ||
          (i < (size / 2).toInt &&
            j == size - 1) ||
          (i > (size / 2).toInt &&
            j == 0) ||
          (j == ((size) / 2).toInt) ||
          (i == size - 1 &&
            j > ((size) / 2).toInt)) {
          print(" *");
        } else {
          //include two space
          print("  ");
        }
        j += 1;
      }
      print("\n");
      i += 1;
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val obj: MyPattern = new MyPattern();
    obj.swastika_pattern(5);
    obj.swastika_pattern(7);
    obj.swastika_pattern(9);
    obj.swastika_pattern(11);
  }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 * * * * * *         *
           *         *
           *         *
           *         *
           *         *
 * * * * * * * * * * *
 *         *
 *         *
 *         *
 *         *
 *         * * * * * *
/*
  Swift Program
  Display Wave Patterns
*/
class MyPattern {
  //Displaying mirror image of swastika pattern in given size
  func swastika_pattern(_ size: Int) {
    if (size <= 3 ||
      size % 2 == 0) {
      return;
    }
    print("\n Size : ", size ,"\n\n", terminator: "");
    var i = 0;
    var j = 0;
    while (i < size) {
      j = 0;
      while (j < size) {
        //Simple test the boundary cases

        if ((i == 0 &&
            j < size / 2) ||
          (i == (size / 2)) ||
          (i < size / 2 &&
            j == size - 1) ||
          (i > size / 2 &&
            j == 0) ||
          (j == (size) / 2) ||
          (i == size - 1 &&
            j > (size) / 2)) {
          print(" *", terminator: "");
        } else {
          print("  ", terminator: "");
        }
        j += 1;
      }
      print("\n", terminator: "");
      i += 1;
    }
  }
}
func main() {
  let obj = MyPattern();
  obj.swastika_pattern(5);
  obj.swastika_pattern(7);
  obj.swastika_pattern(9);
  obj.swastika_pattern(11);
}
main();

Output

 Size :  5

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

 Size :  7

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

 Size :  9

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

 Size :  11

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

Output Explanation:

The output of the program displays the mirror image of the Swastika pattern for different sizes. Each pattern is enclosed within a box made up of asterisks (*). The asterisks (*) represent the outline of the Swastika symbol, while the spaces represent the empty spaces within the symbol.

For example, for a pattern of size 5, the output is as follows:

 Size: 5

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

The pattern is symmetrical along the vertical axis, creating a mirror image of the Swastika. The pattern is formed by printing asterisks (*) in specific positions based on the conditions in the code.

Time Complexity:

The time complexity of this program is O(n^2), where n represents the size of the pattern. This is because the program uses two nested loops to iterate over each position in the pattern, resulting in a quadratic time complexity.

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