Posted on by Kalkicode
Code Pattern

Draw Swastika Pattern

The swastika pattern is a symbol that has been used for thousands of years in various cultures around the world. It is a geometric pattern that consists of four arms that are bent at right angles and point in a clockwise or counterclockwise direction.

In Hinduism, the swastika is considered a sacred symbol and represents good luck, prosperity, and well-being. It is often used in religious ceremonies, and can be seen in many Hindu temples and homes.

In Buddhism, the swastika is known as the "Wheel of Law" and is used to represent the Buddha's teachings. It is often depicted on Buddhist temples and is a symbol of peace, love, and harmony.

Swastika Pattern

In this article, we will discuss how to draw a swastika pattern using Java programming language. The swastika is a symbol that holds religious and cultural significance in various traditions. It consists of an equilateral cross with arms bent at right angles. We will create a Java program that generates the swastika pattern based on a given size.

Problem Statement

The problem is to create a program that takes a size as input and displays a swastika pattern of that size. The size determines the dimensions of the swastika pattern, and it should be an odd number greater than 3.

For example, if the size is 5, the program should generate the following pattern:

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

Algorithm

1. Start the program.

2. Define a class named "MyPattern" that contains the "swastikaPattern" method.

3. In the "swastikaPattern" method:

a. Check if the size is less than or equal to 3 or if it is an even number. If true, return as it is an invalid size for the pattern.

b. Print the size of the pattern.

c. Use nested loops to iterate over each position in the pattern.

d. Inside the loops, check the conditions to determine whether to print an asterisk (*) or a space ( ).

e. Print the asterisk (*) if it falls within the boundary cases that form the swastika shape, otherwise print a space ( ).

f. After each row is printed, include a new line character to move to the next row.

4. Define the "main" method in the "MyPattern" class.

5. Create an instance of the "MyPattern" class.

6. Test the "swastikaPattern" method by calling it with different sizes (e.g., 5, 7, 9, 11).

7. End the program.

Pseudocode


class MyPattern
    method swastikaPattern(size)
        if (size <= 3 OR size is even)
            return
        print("Size: " + size)
        for i = 0 to size
            for j = 0 to size
                if ((i < size / 2 AND j == 0) OR
                    (j == size / 2) OR
                    (i == size / 2) OR
                    (i == 0 AND j > (size / 2)) OR
                    (i == size - 1 AND j < size / 2) OR
                    (i > size / 2 AND j == size - 1))
                    print(" *")
                else
                    print("  ")
            print("\n")
    end method

    method main(args)
        task = new MyPattern()
        task.swastikaPattern(5)
        task.swastikaPattern(7)
        task.swastikaPattern(9)
        task.swastikaPattern(11)
    end method
end class

Code Solution

/*
  Java Program
  Display Wave Patterns
*/
public class MyPattern
{
    // Displaying of swastika Pattern in given size
    public void swastikaPattern(int size)
    {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        System.out.print("\n Size : " + size + "\n\n");
        for (int i = 0; i < size; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                // Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1))
                {
                    System.out.print(" *");
                }
                else
                {
                    System.out.print("  ");
                }
            }
            //include new line
            System.out.print("\n");
        }
    }
    public static void main(String[] args)
    {
        MyPattern task = new MyPattern();
        // Test 
        task.swastikaPattern(5);
        task.swastikaPattern(7);
        task.swastikaPattern(9);
        task.swastikaPattern(11);
    }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Display Wave Patterns
*/
class MyPattern
{
    public:
        // Displaying of swastika Pattern in given size
        void swastikaPattern(int size)
        {
            if (size <= 3 || size % 2 == 0)
            {
                // When get a invalid size of pattern
                return;
            }
            cout << "\n Size : " << size << "\n\n";
            for (int i = 0; i < size; ++i)
            {
                for (int j = 0; j < size; ++j)
                {
                    // Simple test the boundary cases
                    if ((i < size / 2 && j == 0) || 
                        (j == size / 2) ||
                        (i == size / 2) || 
                        (i == 0 && j > (size / 2)) || 
                        (i == size - 1 && j < size / 2) || 
                        (i > size / 2 && j == size - 1))
                    {
                        cout << " *";
                    }
                    else
                    {
                        cout << "  ";
                    }
                }
                //include new line
                cout << "\n";
            }
        }
};
int main()
{
    MyPattern *task = new MyPattern();
    // Test 
    task->swastikaPattern(5);
    task->swastikaPattern(7);
    task->swastikaPattern(9);
    task->swastikaPattern(11);
    return 0;
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
// C Program 
// Draw Swastika Pattern
#include <stdio.h>
#include <stdlib.h>

// Displaying of swastika Pattern in given size
void swastika_pattern(int size)
{
    if (size <= 3 || size % 2 == 0)
    {
        // When get a invalid size of pattern
        return;
    }
    printf("\n Size : %d\n\n", size);
    for (int i = 0; i < size; ++i)
    {
        for (int j = 0; j < size; ++j)
        {
            // Simple test the boundary cases
            if ((i < size / 2 && j == 0) || 
                (j == size / 2) || (i == size / 2) || 
                (i == 0 && j > (size / 2)) || 
                (i == size - 1 && j < size / 2) || 
                (i > size / 2 && j == size - 1))
            {
                printf(" *");
            }
            else
            {
                printf("  ");
            }
        }
        //include new line
        printf("\n");
    }
}
int main()
{
    //Test Cases
    swastika_pattern(5);
    swastika_pattern(7);
    swastika_pattern(9);
    swastika_pattern(11);
    return 0;
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
package main
import "fmt"
/*
  Go Program
  Display Wave Patterns
*/

// Displaying of swastika Pattern in given size
func swastikaPattern(size int) {
    if size <= 3 || size % 2 == 0 {
        // When get a invalid size of pattern
        return
    }
    fmt.Print("\n Size : ", size, "\n\n")
    for i := 0 ; i < size ; i++ {
        for j := 0 ; j < size ; j++ {
            // Simple test the boundary cases
            if (i < size / 2 && j == 0) || 
            (j == size / 2) || 
            (i == size / 2) || 
            (i == 0 && j > (size / 2)) || 
            (i == size - 1 && j < size / 2) || 
            (i > size / 2 && j == size - 1) {
                fmt.Print(" *")
            } else {
                fmt.Print("  ")
            }
        }
        //include new line
        fmt.Print("\n")
    }
}
func main() {

    // Test 
    swastikaPattern(5)
    swastikaPattern(7)
    swastikaPattern(9)
    swastikaPattern(11)
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
// Include namespace system
using System;
/*
  Csharp Program
  Display Wave Patterns
*/
public class MyPattern
{
    // Displaying of swastika Pattern in given size
    public void swastikaPattern(int size)
    {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        Console.Write("\n Size : " + size + "\n\n");
        for (int i = 0; i < size; ++i)
        {
            for (int j = 0; j < size; ++j)
            {
                // Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1))
                {
                    Console.Write(" *");
                }
                else
                {
                    Console.Write("  ");
                }
            }
            //include new line
            Console.Write("\n");
        }
    }
    public static void Main(String[] args)
    {
        MyPattern task = new MyPattern();
        // Test 
        task.swastikaPattern(5);
        task.swastikaPattern(7);
        task.swastikaPattern(9);
        task.swastikaPattern(11);
    }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
<?php
/*
  Php Program
  Display Wave Patterns
*/
class MyPattern
{
    // Displaying of swastika Pattern in given size
    public  function swastikaPattern($size)
    {
        if ($size <= 3 || $size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        print_r("\n Size : ".strval($size).
            "\n\n");
        for ($i = 0; $i < $size; ++$i)
        {
            for ($j = 0; $j < $size; ++$j)
            {
                // Simple test the boundary cases
                if (($i < (int)($size / 2) && $j == 0) || 
                    ($j == (int)($size / 2)) || 
                    ($i == (int)($size / 2)) || 
                    ($i == 0 && $j > ((int)($size / 2))) || 
                    ($i == $size - 1 && $j < (int)($size / 2)) || 
                    ($i > (int)($size / 2) && $j == $size - 1))
                {
                    print_r(" *");
                }
                else
                {
                    print_r("  ");
                }
            }
            //include new line
            print_r("\n");
        }
    }
    public static
    function main($args)
    {
        $task = new MyPattern();
        // Test 
        $task->swastikaPattern(5);
        $task->swastikaPattern(7);
        $task->swastikaPattern(9);
        $task->swastikaPattern(11);
    }
}
MyPattern::main(array());

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
/*
  Node JS Program
  Display Wave Patterns
*/
class MyPattern
{
    // Displaying of swastika Pattern in given size
    swastikaPattern(size)
    {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        process.stdout.write("\n Size : " + size + "\n\n");
        for (var i = 0; i < size; ++i)
        {
            for (var j = 0; j < size; ++j)
            {
                // Simple test the boundary cases
                if ((i < parseInt(size / 2) && j == 0) || 
                    (j == parseInt(size / 2)) || 
                    (i == parseInt(size / 2)) || 
                    (i == 0 && j > (parseInt(size / 2))) ||
                    (i == size - 1 && j < parseInt(size / 2)) || 
                    (i > parseInt(size / 2) && j == size - 1))
                {
                    process.stdout.write(" *");
                }
                else
                {
                    process.stdout.write("  ");
                }
            }
            //include new line
            process.stdout.write("\n");
        }
    }
}

function main()
{
    var task = new MyPattern();
    // Test 
    task.swastikaPattern(5);
    task.swastikaPattern(7);
    task.swastikaPattern(9);
    task.swastikaPattern(11);
}
main();

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
#  Python 3 Program
#  Display Wave Patterns
class MyPattern :
    #  Displaying of swastika Pattern in given size
    def swastikaPattern(self, size) :
        if (size <= 3 or size % 2 == 0) :
            #  When get a invalid size of pattern
            return
        
        print("\n Size : ", size ,"\n")
        i = 0
        while (i < size) :
            j = 0
            while (j < size) :
                #  Simple test the boundary cases
                if ((i < int(size / 2) and j == 0) or
                    (j == int(size / 2)) or
                (i == int(size / 2)) or
                (i == 0 and j > (int(size / 2))) or
                (i == size - 1 and j < int(size / 2)) or
                (i > int(size / 2) and j == size - 1)) :
                    print(" *", end = "")
                else :
                    print("  ", end = "")
                
                j += 1
            
            # include new line
            print(end = "\n")
            i += 1
        
    

def main() :
    task = MyPattern()
    #  Test 
    task.swastikaPattern(5)
    task.swastikaPattern(7)
    task.swastikaPattern(9)
    task.swastikaPattern(11)

if __name__ == "__main__": main()

Output

 Size :  5

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

 Size :  7

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

 Size :  9

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

 Size :  11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
#  Ruby Program
#  Display Wave Patterns
class MyPattern 
    #  Displaying of swastika Pattern in given size
    def swastikaPattern(size) 
        if (size <= 3 || size % 2 == 0) 
            #  When get a invalid size of pattern
            return
        end

        print("\n Size : ", size ,"\n\n")
        i = 0
        while (i < size) 
            j = 0
            while (j < size) 
                #  Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1)) 
                    print(" *")
                else
 
                    print("  ")
                end

                j += 1
            end

            # include new line
            print("\n")
            i += 1
        end

    end

end

def main() 
    task = MyPattern.new()
    #  Test 
    task.swastikaPattern(5)
    task.swastikaPattern(7)
    task.swastikaPattern(9)
    task.swastikaPattern(11)
end

main()

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *          
 *         *          
 *         *          
 *         *          
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
/*
  Scala Program
  Display Wave Patterns
*/
class MyPattern()
{
    // Displaying of swastika Pattern in given size
    def swastikaPattern(size: Int): Unit = {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        print("\n Size : " + size + "\n\n");
        var i: Int = 0;
        while (i < size)
        {
            var j: Int = 0;
            while (j < size)
            {
                // Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1))
                {
                    print(" *");
                }
                else
                {
                    print("  ");
                }
                j += 1;
            }
            //include new line
            print("\n");
            i += 1;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: MyPattern = new MyPattern();
        // Test 
        task.swastikaPattern(5);
        task.swastikaPattern(7);
        task.swastikaPattern(9);
        task.swastikaPattern(11);
    }
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
/*
  Swift 4 Program
  Display Wave Patterns
*/
class MyPattern
{
    // Displaying of swastika Pattern in given size
    func swastikaPattern(_ size: Int)
    {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        print("\n Size : ", size ,"\n");
        var i: Int = 0;
        while (i < size)
        {
            var j: Int = 0;
            while (j < size)
            {
                // Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || 
                    (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1))
                {
                    print(" *", terminator: "");
                }
                else
                {
                    print("  ", terminator: "");
                }
                j += 1;
            }
            //include new line
            print(terminator: "\n");
            i += 1;
        }
    }
}
func main()
{
    let task: MyPattern = MyPattern();
    // Test 
    task.swastikaPattern(5);
    task.swastikaPattern(7);
    task.swastikaPattern(9);
    task.swastikaPattern(11);
}
main();

Output

 Size :  5

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

 Size :  7

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

 Size :  9

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

 Size :  11

 *         * * * * * *
 *         *
 *         *
 *         *
 *         *
 * * * * * * * * * * *
           *         *
           *         *
           *         *
           *         *
 * * * * * *         *
/*
  Kotlin Program
  Display Wave Patterns
*/
class MyPattern
{
    // Displaying of swastika Pattern in given size
    fun swastikaPattern(size: Int): Unit
    {
        if (size <= 3 || size % 2 == 0)
        {
            // When get a invalid size of pattern
            return;
        }
        print("\n Size : " + size + "\n\n");
        var i: Int = 0;
        while (i < size)
        {
            var j: Int = 0;
            while (j < size)
            {
                // Simple test the boundary cases
                if ((i < size / 2 && j == 0) || 
                    (j == size / 2) || 
                    (i == size / 2) || 
                    (i == 0 && j > (size / 2)) || 
                    (i == size - 1 && j < size / 2) || 
                    (i > size / 2 && j == size - 1))
                {
                    print(" *");
                }
                else
                {
                    print("  ");
                }
                j += 1;
            }
            //include new line
            print("\n");
            i += 1;
        }
    }
}
fun main(args: Array < String > ): Unit
{
    val task: MyPattern = MyPattern();
    // Test 
    task.swastikaPattern(5);
    task.swastikaPattern(7);
    task.swastikaPattern(9);
    task.swastikaPattern(11);
}

Output

 Size : 5

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

 Size : 7

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

 Size : 9

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

 Size : 11

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

Output Explanation

The program generates the swastika pattern for different sizes. Here is the explanation for each output:

For size 5:

 Size: 5

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

For size 7:

 Size: 7

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

For size 9:

 Size: 9

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

For size 11:

 Size: 11

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

Time Complexity

The time complexity of the program is O(n^2), where n represents the size of the pattern. The program uses nested loops to iterate over each position in the pattern, resulting in a quadratic time complexity.

By following the provided algorithm and pseudocode, you can draw the swastika pattern of various sizes using Java programming language.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment