Horje
Program to print Swastika Pattern

Given the number of rows and columns, print the corresponding swastika pattern using loops. 

Note: The number of rows and columns should be the same and an odd number. This will generate a perfect swastika pattern. 

Examples : 
 

Input : row = 7, column = 7        
Output:
*     * * * *
*     *
*     *
* * * * * * *
      *     *
      *     *
* * * *     *

Input : row = 11, column = 11
Output :
*         * * * * * *
*         *
*         *
*         *
*         *
* * * * * * * * * * * 
          *         * 
          *         * 
          *         * 
          *         * 
* * * * * *         * 

Recommended: Please try your approach on {IDE} first, before moving on a solution.

Below is the implementation to print the swastika pattern. 
 

C++

<?php
// PHP implementation to
// print swastika pattern
 
// function to print swastika
function swastika($row, $col)
{
for ($i = 0; $i < $row; $i++)
{
    for ($j = 0; $j < $col; $j++)
    {
         
    // checking if i < row/2
    if ($i < floor($row / 2))
    {
         
        // checking if j<col/2
        if ($j < floor($col / 2))
        {
             
        // print '*' if j=0
        if ($j == 0)
            echo "*";
             
        // else print space
        else
            echo " " . " ";
        }
         
        // check if j=col/2
        else if ($j == floor($col / 2))
        echo " *";
        else
        {
        // if i=0 then first
        // row will have '*'
        if ($i == 0)
            echo " *";
        }
    }
    else if ($i == floor($row / 2))
        echo "* ";
    else
    {
         
        // middle column and last
        // column will have '*'
        // after i > row/2
        if ($j == floor($col / 2 )||
            $j == $col - 1)
        echo "* ";
         
        // last row
        else if ($i == $row - 1)
        {
             
        // last row will be have
        // '*' if j <= col/2 or
        // if it is last column
        if ($j <= floor($col / 2) ||
            $j == $col - 1)
            echo "* ";
        else
            echo " " . " ";
        }
        else
        echo " " . " ";
    }
    }
    echo "\n";
}
}
 
// Driver Code
 
// odd number of row
// and column to get
// perfect swastika
$row = 7;
$col = 7;
     
// function calling
swastika($row, $col);
     
// This code is contributed by ajit
?>

Javascript

<script>
      // JavaScript implementation to
      // print swastika pattern
 
      // function to print swastika
      function swastika(row, col)
      {
        for (var i = 0; i < row; i++)
        {
          for (var j = 0; j < col; j++)
          {
           
            // checking if i < row/2
            if (i < Math.floor(row / 2))
            {
             
              // checking if j<col/2
              if (j < Math.floor(col / 2))
              {
               
                // print '*' if j=0
                if (j == 0) document.write("*");
                 
                // else print space
                else document.write("  " + " ");
              }
 
              // check if j=col/2
              else if (j == Math.floor(col / 2)) document.write(" *");
              else
              {
               
                // if i=0 then first row will have '*'
                if (i == 0) document.write(" *");
              }
            } else if (i == Math.floor(row / 2)) document.write("* ");
            else
            {
             
              // middle column and last column will have '*'
              // after i > row/2
              if (j == Math.floor(col / 2) || j == col - 1)
                document.write("* ");
                 
              // last row
              else if (i == row - 1)
              {
               
                // last row will be have '*' if
                // j <= col/2 or if it is last column
                if (j <= Math.floor(col / 2) || j == col - 1)
                  document.write("* ");
                else document.write("  " + " ");
              } else document.write("  " + " ");
            }
          }
          document.write("<br>");
        }
      }
 
      // driver code
      // odd number of row and column
      // to get perfect swastika
      var row = 7,
        col = 7;
         
      // function calling
      swastika(row, col);
       
      // This code is contributed by rdtank.
    </script>

Output: 
 

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

Time Complexity: O(row * col), where row and col represents the given number of rows and columns.
Auxiliary Space: O(1), no extra space is required, so it is a constant.




Reffered: https://www.geeksforgeeks.org


C++ Programs

Related
Program to convert IP address to hexadecimal Program to convert IP address to hexadecimal
C++ program to read file word by word C++ program to read file word by word
C++ program for Solving Cryptarithmetic Puzzles C++ program for Solving Cryptarithmetic Puzzles
StringStream in C++ for Decimal to Hexadecimal and back StringStream in C++ for Decimal to Hexadecimal and back
Calculate range of data types using C++ Calculate range of data types using C++

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9