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