Program to print mirror image of Swastika Pattern
Last Updated :
20 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void revswastika( int row, int col)
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++) {
if (i < row / 2) {
if (j < col / 2) {
if (i == 0 && i < row / 2)
cout << "*" << " " ;
else
cout << " " << " " ;
}
else if (j == col / 2)
cout << "*" ;
else {
if (i < row / 2 && j < col - 1)
cout << " " << " " ;
if (j == col - 1)
cout << " " << "*" ;
}
}
else if (i == row / 2)
cout << "*" << " " ;
else
{
if (j == col / 2 || j == 0)
cout << "*" << " " ;
else if (i == row - 1)
{
if (j <= col / 2 || j == col)
cout << " " << " " ;
else
cout << "*" << " " ;
}
else
cout << " " << " " ;
}
}
cout << "\n" ;
}
}
int main()
{
int row = 9, col = 9;
revswastika(row, col);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void revswastika( int row, int col)
{
for ( int i = 0 ; i < row; i++)
{
for ( int j = 0 ; j < col; j++)
{
if (i < row / 2 )
{
if (j < col / 2 )
{
if (i == 0 && i < row / 2 )
System.out.print( "*" + " " );
else
System.out.print( " " + " " );
}
else if (j == col / 2 )
System.out.print( "*" );
else
{
if (i < row / 2 && j < col - 1 )
System.out.print( " " + " " );
if (j == col - 1 )
System.out.print( " " + "*" );
}
}
else if (i == row / 2 )
System.out.print( "*" + " " );
else
{
if (j == col / 2 || j == 0 )
System.out.print( "*" + " " );
else if (i == row - 1 )
{
if (j <= col / 2 || j == col)
System.out.print ( " " + " " );
else
System.out.print( "*" + " " );
}
else
System.out.print( " " + " " );
}
}
System.out.println();
}
}
public static void main (String[] args)
{
int row = 9 , col = 9 ;
revswastika(row, col);
}
}
|
Python3
def revswastika(row, col):
for i in range (row):
for j in range (col):
if (i < row / / 2 ):
if (j < col / / 2 ):
if (i = = 0 and i < row / / 2 ):
print ( "* " , end = "")
else :
print ( " " , end = " " )
elif (j = = col / / 2 ):
print ( "*" , end = "")
else :
if (i < row / / 2 and j < col - 1 ):
print ( " " , end = " " )
if (j = = col - 1 ):
print ( " *" , end = "")
elif (i = = row / / 2 ):
print ( "* " , end = "")
else :
if (j = = col / / 2 or j = = 0 ):
print ( "* " , end = "")
elif (i = = row - 1 ):
if (j < = col / / 2 or j = = col):
print ( " " , end = " " )
else :
print ( "* " , end = "")
else :
print ( " " , end = " " )
print ()
row = 9 ; col = 9
revswastika(row, col)
|
C#
using System;
class GFG {
static void revswastika( int row, int col)
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
{
if (i < row / 2)
{
if (j < col / 2)
{
if (i == 0 && i < row / 2)
Console.Write( "*" + " " );
else
Console.Write( " " + " " );
}
else if (j == col / 2)
Console.Write( "*" );
else
{
if (i < row / 2 && j < col - 1)
Console.Write( " " + " " );
if (j == col - 1)
Console.Write( " " + "*" );
}
}
else if (i == row / 2)
Console.Write( "*" + " " );
else
{
if (j == col / 2 || j == 0)
Console.Write( "*" + " " );
else if (i == row - 1)
{
if (j <= col / 2 || j == col)
Console.Write( " " + " " );
else
Console.Write( "*" + " " );
}
else
Console.Write( " " + " " );
}
}
Console.WriteLine();
}
}
public static void Main ()
{
int row = 9, col = 9;
revswastika(row, col);
}
}
|
PHP
<?php
function revswastika( $row , $col )
{
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $col ; $j ++) {
if ( $i < floor ( $row / 2))
{
if ( $j < floor ( $col / 2))
{
if ( $i == 0 && $i < floor ( $row / 2))
echo "*" . " " ;
else
echo " " . " " ;
}
else if ( $j == floor ( $col / 2))
echo "*" ;
else {
if ( $i < floor ( $row / 2) &&
$j < $col - 1)
echo " " . " " ;
if ( $j == $col - 1)
echo " " . "*" ;
}
}
else if ( $i == floor ( $row / 2))
echo "*" . " " ;
else
{
if ( $j == floor ( $col / 2) || $j == 0)
echo "*" . " " ;
else if ( $i == $row - 1)
{
if ( $j <= floor ( $col / 2) ||
$j == $col )
echo " " . " " ;
else
echo "*" . " " ;
}
else
echo " " . " " ;
}
}
echo "\n" ;
}
}
$row = 9;
$col = 9;
revswastika( $row , $col );
?>
|
Javascript
<script>
function revswastika(row, col)
{
for ( var i = 0; i < row; i++)
{
for ( var j = 0; j < col; j++)
{
if (i < Math.floor(row / 2))
{
if (j < Math.floor(col / 2))
{
if (i == 0 && i < Math.floor(row / 2))
document.write( "*" + " " );
else document.write( " " + " " );
}
else if (j == Math.floor(col / 2)) document.write( "*" );
else
{
if (i < Math.floor(row / 2) && j < col - 1)
document.write( " " + " " );
if (j == col - 1)
document.write( " " + "*" );
}
}
else if (i == Math.floor(row / 2))
document.write( "*" + " " );
else
{
if (j == Math.floor(col / 2) || j == 0)
document.write( "*" + " " );
else if (i == row - 1)
{
if (j <= Math.floor(col / 2) || j == col)
document.write( " " + " " );
else document.write( "*" + " " );
}
else document.write( " " + " " );
}
}
document.write( "<br>" );
}
}
var row = 9,
col = 9;
revswastika(row, col);
</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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...