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 ?> |
Javascript
<script> // JavaScript implementation to print // mirror image of swastika pattern. // function to print mirror image of swastika function revswastika(row, col) { for ( var i = 0; i < row; i++) { for ( var j = 0; j < col; j++) { // for the upper half of the Swastika if (i < Math.floor(row / 2)) { // checking if j < col/2 if (j < Math.floor(col / 2)) { // the first quadrant will have // stars till half the number of rows if (i == 0 && i < Math.floor(row / 2)) document.write( "*" + " " ); // rest will have spaces else document.write( " " + " " ); } // the middle line of the swastika else if (j == Math.floor(col / 2)) document.write( "*" ); else { // the second quadrant of the // upper half will have spaces if (i < Math.floor(row / 2) && j < col - 1) document.write( " " + " " ); if (j == col - 1) // only the last column of the // second quadrant will have stars document.write( " " + "*" ); } } // for the lower half of the Swastika // the middle horizontal line of the // Swastika will be at row/2 else if (i == Math.floor(row / 2)) document.write( "*" + " " ); else { // for stars in the third quadrant // and the middle line of the lower Swastika if (j == Math.floor(col / 2) || j == 0) document.write( "*" + " " ); else if (i == row - 1) { // the last row's third quadrant // will have spaces and the fourth // will have stars if (j <= Math.floor(col / 2) || j == col) document.write( " " + " " ); else document.write( "*" + " " ); } // rest of the remaining portion // will have spaces all along else document.write( " " + " " ); } } document.write( "<br>" ); } } // Driver code // same no. of rows and columns to // get the perfect mirror image // of swastika pattern. var row = 9, col = 9; // function calling revswastika(row, col); // This code is contributed by rdtank. </script> |
Output :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Time Complexity : O(row*col) ,where row is number of rows and col is number of columns in reverse swastika pattern.
Space Complexity : O(1) ,as we are not using any extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!