Skip to content
Related Articles

Related Articles

Program to print mirror image of Swastika Pattern
  • Difficulty Level : Easy
  • Last Updated : 18 Apr, 2018

Given the number of rows and columns, print the mirror image of swastika pattern.

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

Input : row = 5, col = 5
Output :
* * *   *
    *   *
* * * * *
*   *
*   * * *

Input : row = 9, col = 9 
Output :
* * * * *       *
        *       *
        *       *
        *       *
* * * * * * * * *
*       *
*       *
*       *
*       * * * * *

Below is the implementation to print the mirror image of swastika pattern.

C++




// C++ implementation to print
// mirror image of swastika pattern.
#include <bits/stdc++.h>
using namespace std;
  
// function to print mirror image of swastika
void revswastika(int row, int col) 
{
for (int i = 0; i < row; i++) 
{
    for (int j = 0; j < col; j++) {
          
    // for the upper half of the Swastika
    if (i < row / 2) {
          
        // checking if j < col/2
        if (j < col / 2) {
              
        // the first quadrant will have
        // stars till half the number of rows
        if (i == 0 && i < row / 2)
            cout << "*" << " ";
              
        // rest will have spaces
        else
            cout << " " << " ";
        }
          
        // the middle line of the swastika
        else if (j == col / 2)
        cout << "*";
        else {
              
        // the second quadrant of the
        // upper half will have spaces
        if (i < row / 2 && j < col - 1)
            cout << " " << " ";
        if (j == col - 1)
          
            // only the last column of the
            // second quadrant will have stars
            cout << " "    << "*";
        }
    }
  
    // for the lower half of the Swastika
    // the middle horizontal line of the
    // Swastika will be at row/2
    else if (i == row / 2)
        cout << "*" << " ";
    else 
    {
        // for stars in the third quadrant
        // and the middle line of the lower Swastika
        if (j == col / 2 || j == 0)
        cout << "*" << " ";
        else if (i == row - 1)
        {
        // the last row's third quadrant
        // will have spaces and the fourth
        // will have stars
        if (j <= col / 2 || j == col)
            cout << " " << " ";
        else
            cout << "*" << " ";
        }
          
        // rest of the remaining portion
        // will have spaces all along
        else
        cout << " " << " ";
    }
    }
    cout << "\n";
}
}
  
// Driver code
int main()
{
   // same no. of rows and columns to
   // get the perfect mirror image
   // of swastika pattern.
   int row = 9, col = 9;
  
   // function calling
   revswastika(row, col);
  
   return 0;
}


Java




// Java implementation to print
// mirror image of swastika pattern.
  
import java.io.*;
  
class GFG 
{
    // function to print mirror image of swastika
    static void revswastika(int row, int col) 
    {
        for (int i = 0; i < row; i++) 
        {
            for (int j = 0; j < col; j++)
            {
                  
                // for the upper half of the Swastika
                if (i < row / 2
                {
                      
                    // checking if j < col/2
                    if (j < col / 2
                    {
                        // the first quadrant will have
                        // stars till half the number of rows
                        if (i == 0 && i < row / 2)
                            System.out.print("*" + " ");
                              
                        // rest will have spaces
                        else
                            System.out.print(" " + " ");
                    }
                      
                    // the middle line of the swastika
                    else if (j == col / 2)
                       System.out.print("*");
                    else
                    {
                        // the second quadrant of the
                        // upper half will have spaces
                        if (i < row / 2 && j < col - 1)
                            System.out.print(" " + " ");
                        if (j == col - 1)
                          
                            // only the last column of the
                            // second quadrant will have stars
                            System.out.print(" " + "*");
                    }
                }
          
                // for the lower half of the Swastika
                // the middle horizontal line of the
                // Swastika will be at row/2
                else if (i == row / 2)
                    System.out.print("*" + " ");
                else
                {
                    // for stars in the third quadrant
                    // and the middle line of the lower Swastika
                    if (j == col / 2 || j == 0)
                        System.out.print( "*" + " ");
                    else if (i == row - 1)
                    {
                        // the last row's third quadrant
                        // will have spaces and the fourth
                        // will have stars
                        if (j <= col / 2 || j == col)
                            System.out.print (" " + " ");
                        else
                            System.out.print("*" + " ");
                    }
                      
                    // rest of the remaining portion
                    // will have spaces all along
                    else
                    System.out.print(" " + " ");
                }
            }
            System.out.println();
        }
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        // same no. of rows and columns to
        // get the perfect mirror image
        // of swastika pattern.
        int row = 9, col = 9;
          
        // function calling
        revswastika(row, col);
              
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 implementation to print
# mirror image of swastika pattern.
  
# Function to print mirror image of swastika
def revswastika(row, col):
      
    for i in range(row): 
        for j in range(col):
              
            # for the upper half of the Swastika
            if (i < row // 2):
                  
                # checking if j < col/2
                if (j < col // 2):
                      
                    # The first quadrant will have
                    # stars till half the number of rows
                    if (i == 0 and i < row // 2):
                        print("* ", end = "")
                          
                    # rest will have spaces
                    else:
                        print(" ", end = " ")
                          
                # the middle line of the swastika
                elif (j == col // 2):
                    print("*", end = "")
                else:
                      
                    # the second quadrant of the
                    # upper half will have spaces
                    if (i < row // 2 and j < col - 1):
                        print(" ", end = " ")
                    if (j == col - 1):
                          
                        # only the last column of the
                        # second quadrant will have stars
                        print(" *", end = "")
                          
               # For the lower half of the Swastika
            # the middle horizontal line of the
            # Swastika will be at row/2
            elif (i == row // 2):
                print("* ", end = "")
            else:
                  
                # For stars in the third quadrant and
                # the middle line of the lower Swastika
                if (j == col // 2 or j == 0):
                    print("* ", end = "")
                elif (i == row - 1):
                      
                    # last row's third quadrant will
                    # have spaces and fourth will have stars
                    if (j <= col // 2 or j == col):
                        print(" ", end = " ")
                    else:
                        print("* ", end = "")
                          
                # rest of the remaining portion
                # will have spaces all along
                else:
                    print(" ", end = " ")
        print()
          
# Driver code
  
# same no. of rows and columns to
# get the perfect mirror image
# of swastika pattern.
row = 9; col = 9
  
revswastika(row, col)
  
# This code is contributed by Azkia Anam.


C#




// C# implementation to print mirror 
// image of swastika pattern.
using System;
  
class GFG {
      
    // function to print mirror image 
    // of swastika
    static void revswastika(int row, int col) 
    {
        for (int i = 0; i < row; i++) 
        {
            for (int j = 0; j < col; j++)
            {
                  
                // for the upper half of the
                // Swastika
                if (i < row / 2) 
                {
                      
                    // checking if j < col/2
                    if (j < col / 2) 
                    {
                        // the first quadrant 
                        // will have stars till 
                        // half the number of
                        // rows
                        if (i == 0 && i < row / 2)
                            Console.Write("*" + " ");
                              
                        // rest will have spaces
                        else
                            Console.Write(" " + " ");
                    }
                      
                    // the middle line of the
                    // swastika
                    else if (j == col / 2)
                        Console.Write("*");
                    else
                    {
                        // the second quadrant of 
                        // the upper half will 
                        // have spaces
                        if (i < row / 2 && j < col - 1)
                            Console.Write(" " + " ");
                        if (j == col - 1)
                          
                            // only the last column 
                            // of the second quadrant 
                            // will have stars
                            Console.Write(" " + "*");
                    }
                }
          
                // for the lower half of the Swastika
                // the middle horizontal line of the
                // Swastika will be at row/2
                else if (i == row / 2)
                    Console.Write("*" + " ");
                else
                {
                    // for stars in the third quadrant
                    // and the middle line of the 
                    // lower Swastika
                    if (j == col / 2 || j == 0)
                        Console.Write( "*" + " ");
                    else if (i == row - 1)
                    {
                        // the last row's third quadrant
                        // will have spaces and the fourth
                        // will have stars
                        if (j <= col / 2 || j == col)
                            Console.Write(" " + " ");
                        else
                            Console.Write("*" + " ");
                    }
                      
                    // rest of the remaining portion
                    // will have spaces all along
                    else
                        Console.Write(" " + " ");
                }
            }
              
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main () 
    {
        // same no. of rows and columns to
        // get the perfect mirror image
        // of swastika pattern.
        int row = 9, col = 9;
          
        // function calling
        revswastika(row, col);
              
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP code to print Mirror
// image of swastika pattern.
  
// function to print 
// mirror image of swastika
function revswastika($row, $col
{
for ($i = 0; $i < $row; $i++) 
{
    for ($j = 0; $j < $col; $j++) {
          
    // for the upper half 
    // of the Swastika
    if ($i < floor($row / 2))
    {
          
        // checking if j < $col/2
        if ($j < floor($col / 2)) 
        {
              
        // the first quadrant will 
        // have stars till half the
        // number of $rows
        if ($i == 0 && $i < floor($row / 2))
            echo "*" . " ";
              
        // rest will have spaces
        else
            echo " " . " ";
        }
          
        // the middle line
        // of the swastika
        else if ($j == floor($col / 2))
        echo "*";
        else {
              
            // the second quadrant of the
            // upper half will have spaces
            if ($i < floor($row / 2) && 
                $j < $col - 1)
                echo " " . " ";
            if ($j == $col - 1)
              
                // only the last $column
                // of the second quadrant
                // will have stars
                echo " " . "*";
        }
    }
  
    // for the lower half of the Swastika
    // the middle horizontal line of the
    // Swastika will be at $row/2
    else if ($i == floor($row / 2))
        echo "*" . " ";
    else
    {
          
        // for stars in the third 
        // quadrant and the middle 
        // line of the lower 
        // Swastika
        if ($j == floor($col / 2) || $j == 0)
        echo "*" . " ";
        else if ($i == $row - 1)
        {
            // the last $row's third 
            // quadrant will have spaces 
            // and the fourth will have 
            // stars
            if ($j <= floor($col / 2) || 
                $j == $col)
                echo " " . " ";
            else
                echo "*" . " ";
        }
          
        // rest of the remaining 
        // portion will have spaces
        // all along
        else
        echo " " . " ";
    }
    }
    echo "\n";
}
}
  
// Driver code
// same no. of $rows and $columns 
// to get the perfect mirror image
// of swastika pattern.
$row = 9;
$col = 9;
  
// function calling
revswastika($row, $col);
  
// This code is contributed by mits
?>



Output :

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :