Display mirror image of swastika pattern

Mirror image of swastika pattern

Here given code implementation process.

//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

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


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







© 2021, kalkicode.com, All rights reserved