Draw Swastika Pattern

Swastika Pattern

Here given code implementation process.

/*
  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

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

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







© 2021, kalkicode.com, All rights reserved