Program to print Swastika Pattern - GeeksforGeeks

19 Apr, 2021
Given the number of rows and columns, print the corresponding swastika pattern using loops. Note : The number of rows and columns should be 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 solutionBelow is the implementation to print swastika pattern.  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation to// print swastika pattern#include <bits/stdc++.h>using namespace std; // function to print swastikavoid swastika(int row, int col){for (int i = 0; i < row; i++) {    for (int j = 0; j < col; j++) {             // checking if i < row/2    if (i < row / 2) {                 // checking if j<col/2        if (j < col / 2) {                     // print '*' if j=0        if (j == 0)            cout << "*";                     // else print space        else            cout << " " << " ";        }                 // check if j=col/2        else if (j == col / 2)        cout << " *";        else        {        // if i=0 then first row will have '*'        if (i == 0)            cout << " *";        }    }    else if (i == row / 2)        cout << "* ";    else {                 // middle column and last column will have '*'        // after i > row/2        if (j == col / 2 || j == col - 1)        cout << "* ";                 // last row        else if (i == row - 1) {                     // last row will be have '*' if        // j <= col/2 or if it is last column        if (j <= col / 2 || j == col - 1)            cout << "* ";        else            cout << " " << " ";        }        else        cout << " " << " ";    }    }    cout << "\n";}} // driver codeint main(){   // odd number of row and column   // to get perfect swastika   int row = 7, col = 7;       // function calling   swastika(row, col);       return 0;}
// Java implementation to// print swastika pattern class GFG{    // function to print swastika    static void swastika(int row, int col)    {        for (int i = 0; i < row; i++) {            for (int j = 0; j < col; j++)            {                // checking if i < row/2                if (i < row / 2)                {                                         // checking if j<col/2                    if (j < col / 2)                    {                                                 // print '*' if j=0                        if (j == 0)                            System.out.print("*");                                                     // else print space                        else                            System.out.print(" "+ " ");                    }                                         // check if j=col/2                    else if (j == col / 2)                    System.out.print(" *");                                         else                    {                        // if i=0 then first row                        // will have '*'                        if (i == 0)                            System.out.print(" *");                    }                }                else if (i == row / 2)                    System.out.print("* ");                else                {                                         // middle column and last column                    // will have '*' after i > row/2                    if (j == col / 2 || j == col - 1)                    System.out.print("* ");                                         // last row                    else if (i == row - 1)                    {                                                 // last row will be have '*' if                        // j <= col/2 or if it is last column                        if (j <= col / 2 || j == col - 1)                            System.out.print("* ");                        else                            System.out.print(" "+ " ");                    }                    else                    System.out.print(" "+" ");                }            }            System.out.print("\n");        }    }         // Driver code    public static void main (String[] args)    {        // odd number of row and column        // to get perfect swastika        int row = 7, col = 7;                 // function calling        swastika(row, col);    }} // This code is contributed by Anant Agarwal.
# Python3 implementation to print swastika pattern # Function to print swastikadef swastika(row,col):         for i in range(row):        for j in range(col):                         # checking if i < row/2            if(i < row // 2):                                 # checking if j<col/2                if (j < col // 2):                                         # print '*' if j=0                    if (j == 0):                        print("*", end = "")                                             # else print space                    else:                        print(" ", end = " ")                                 # check if j=col/2                elif (j == col // 2):                    print(" *", end = "")                else:                                         # if i=0 then first row will have '*'                    if (i == 0):                        print(" *", end = "")                                     elif (i == row // 2):                print("* ", end = "")            else:                                 # middle column and last column will                # have '*' after i > row/2                if (j == col // 2 or j == col - 1):                    print("* ", end = "")                                     # last row                elif (i == row - 1):                                         # last row will be have '*' if                    # j <= col/2 or if it is last column                    if (j <= col // 2 or j == col - 1):                        print("* ", end = "")                    else:                        print(" ", end = " ")                else:                    print(" ", end = " ")        print()         # Driver code # odd number of row and column# to get perfect swastikarow = 7; col = 7 # Function callingswastika(row, col) # This code is contributed by Azkia Anam.
// C# implementation to print swastika patternusing System; class GFG {         // function to print swastika    static void swastika(int row, int col)    {        for (int i = 0; i < row; i++) {            for (int j = 0; j < col; j++)            {                                 // checking if i < row/2                if (i < row / 2)                {                                         // checking if j < col/2                    if (j < col / 2)                    {                                                 // print '*' if j = 0                        if (j == 0)                            Console.Write("*");                                                     // else print space                        else                            Console.Write(" "+ " ");                    }                                         // check if j = col/2                    else if (j == col / 2)                        Console.Write(" *");                                         else                    {                        // if i=0 then first row                        // will have '*'                        if (i == 0)                            Console.Write(" *");                    }                }                else if (i == row / 2)                    Console.Write("* ");                else                {                                         // middle column and last column                    // will have '*' after i > row/2                    if (j == col / 2 || j == col - 1)                        Console.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 <= col / 2 || j == col - 1)                            Console.Write("* ");                        else                            Console.Write(" "+ " ");                    }                    else                        Console.Write(" "+" ");                }            }                         Console.WriteLine();        }    }         // Driver code    public static void Main ()    {                 // odd number of row and column        // to get perfect swastika        int row = 7, col = 7;                 // function calling        swastika(row, col);    }} // This code is contributed by vt_m.
<?php// PHP implementation to// print swastika pattern // function to print swastikafunction 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 callingswastika($row, $col);     // This code is contributed by ajit?>
<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:  
*     * * * *
*     *
*     *
* * * * * * *
      *     *
      *     *
* * * *     *
 
jit_t
rdtank
pattern-printing
C Programs
C++ Programs
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
C Program to read contents of Whole File
How to return multiple values from a function in C or C++?
How to Append a Character to a String in C
C program to sort an array in ascending order
Header files in C/C++ and its uses
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Shallow Copy and Deep Copy in C++
Sorting a Map by value in C++ STL