Display 6 pointed star pattern

Six Pointed Star Pattern

Here given code implementation process.

// C Program
// Display 6 pointed star pattern
#include <stdio.h>

//include space 
void space(int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf(" ");
    }
}
//include star 
void star(int size)
{
    for (int i = 0; i < size; ++i)
    {
        if (i % 2 == 0)
        {
            printf("*");
        }
        else
        {
            printf(" ");
        }
    }
}
//Function which is displaying a star shape of given side
void print_star(int side)
{
    if (side < 3)
    {
        return;
    }
    int i = 0;
    printf("\n Side Length : %d\n\n", side);
    //loop which is display the top layer of star pattern
    for (i = 0; i < (side); ++i)
    {
        if (i < side - 1)
        {
            //initial distance using space
            space(side * 2);
            //This is initial space of current position
            space(side - i);
            printf("*");
            if (i != 0)
            {
                space(i + i - 1);
                printf("*");
            }
        }
        else
        {
            //Include last layer of top section
            space(3);
            star(side * 2);
            space((side - 2) * 2);
            star(side * 2);
        }
        printf("\n");
    }
    //This is include middle top layer
    for (i = 0; i < (side)-1; ++i)
    {
        space(i + 4);
        printf("*");
        space((side - 1) * 6 - (i + 3 + i));
        printf("*\n");
    }
    //This is include middle bottom layer
    for (i = 1; i < (side); ++i)
    {
        if (i < side - 1)
        {
            space(side + 2 - i);
            printf("*");
            space((side - 1) * 4 + (i * 2) - 1);
            printf("*");
        }
        else
        {
            space(3);
            star(side * 2);
            space((side - 2) * 2);
            star(side * 2);
        }
        printf("\n");
    }
    //This is include bottom layer
    for (i = 1; i < side; ++i)
    {
        space(3 + (side - 1) * 2 + i);
        printf("*");
        if (i + 1 != side)
        {
            space((side - 1) * 2 - (i) * 2 - 1);
            printf("*");
        }
        printf("\n");
    }
}
int main()
{
    //Test Case
    print_star(3);
    print_star(5);
    print_star(7);
    print_star(4);
    return 0;
}

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
/*
Java Program 
Display 6 pointed star pattern
*/
class MyPattern
{
    //include space 
    public void space(int size)
    {
        for (int i = 0; i < size; ++i)
        {
            System.out.print(" ");
        }
    }
    //include star 
    public void star(int size)
    {
        for (int i = 0; i < size; ++i)
        {
            if (i % 2 == 0)
            {
                System.out.print("*");
            }
            else
            {
                System.out.print(" ");
            }
        }
    }
    //Function which is displaying a star shape of given side
    public void print_star(int side)
    {
        if (side < 3)
        {
            return;
        }
        int i = 0;
        System.out.print("\n Side Length : " + side + "\n\n");
        //loop which is display the top layer of star pattern
        for (i = 0; i < (side); ++i)
        {
            if (i < side - 1)
            {
                //initial distance using space
                space(side * 2);
                //This is initial space of current position
                space(side - i);
                System.out.print("*");
                if (i != 0)
                {
                    space(i + i - 1);
                    System.out.print("*");
                }
            }
            else
            {
                //Include last layer of top section
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            System.out.print("\n");
        }
        //This is include middle top layer
        for (i = 0; i < (side-1); ++i)
        {
            space(i + 4);
            System.out.print("*");
            space((side - 1) * 6 - (i + 3 + i));
            System.out.print("*\n");
        }
        //This is include middle bottom layer
        for (i = 1; i < (side); ++i)
        {
            if (i < side - 1)
            {
                space(side + 2 - i);
                System.out.print("*");
                space((side - 1) * 4 + (i * 2) - 1);
                System.out.print("*");
            }
            else
            {
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            System.out.print("\n");
        }
        //This is include bottom layer
        for (i = 1; i < side; ++i)
        {
            space(3 + (side - 1) * 2 + i);
            System.out.print("*");
            if (i + 1 != side)
            {
                space((side - 1) * 2 - (i) * 2 - 1);
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
    public static void main(String[] args)
    {
        //Make object
        MyPattern obj = new MyPattern();
        //Test Case
        obj.print_star(3);
        obj.print_star(5);
        obj.print_star(7);
        obj.print_star(4);
    }
}

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
/*
C++ Program 
Display 6 pointed star pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
    public:
        //include space 
        void space(int size)
        {
            for (int i = 0; i < size; ++i)
            {
                cout << " ";
            }
        }
    //include star 
    void star(int size)
    {
        for (int i = 0; i < size; ++i)
        {
            if (i % 2 == 0)
            {
                cout << "*";
            }
            else
            {
                cout << " ";
            }
        }
    }
    //Function which is displaying a star shape of given side
    void print_star(int side)
    {
        if (side < 3)
        {
            return;
        }
        int i = 0;
        cout << "\n Side Length : " << side << "\n\n";
        //loop which is display the top layer of star pattern
        for (i = 0; i < (side); ++i)
        {
            if (i < side - 1)
            {
                //initial distance using space
                space(side * 2);
                //This is initial space of current position
                space(side - i);
                cout << "*";
                if (i != 0)
                {
                    space(i + i - 1);
                    cout << "*";
                }
            }
            else
            {
                //Include last layer of top section
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            cout << "\n";
        }
        //This is include middle top layer
        for (i = 0; i < (side-1); ++i)
        {
            space(i + 4);
            cout << "*";
            space((side - 1) * 6 - (i + 3 + i));
            cout << "*\n";
        }
        //This is include middle bottom layer
        for (i = 1; i < (side); ++i)
        {
            if (i < side - 1)
            {
                space(side + 2 - i);
                cout << "*";
                space((side - 1) * 4 + (i * 2) - 1);
                cout << "*";
            }
            else
            {
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            cout << "\n";
        }
        //This is include bottom layer
        for (i = 1; i < side; ++i)
        {
            space(3 + (side - 1) * 2 + i);
            cout << "*";
            if (i + 1 != side)
            {
                space((side - 1) * 2 - (i) * 2 - 1);
                cout << "*";
            }
            cout << "\n";
        }
    }
};
int main()
{
    //Make object
    MyPattern obj ;
    //Test Case
    obj.print_star(3);
    obj.print_star(5);
    obj.print_star(7);
    obj.print_star(4);
    return 0;
}

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
/*
C# Program 
Display 6 pointed star pattern
*/
using System;
class MyPattern
{
    //include space 
    public void space(int size)
    {
        for (int i = 0; i < size; ++i)
        {
            Console.Write(" ");
        }
    }
    //include star 
    public void star(int size)
    {
        for (int i = 0; i < size; ++i)
        {
            if (i % 2 == 0)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write(" ");
            }
        }
    }
    //Function which is displaying a star shape of given side
    public void print_star(int side)
    {
        if (side < 3)
        {
            return;
        }
        int i = 0;
        Console.Write("\n Side Length : " + side + "\n\n");
        //loop which is display the top layer of star pattern
        for (i = 0; i < (side); ++i)
        {
            if (i < side - 1)
            {
                //initial distance using space
                space(side * 2);
                //This is initial space of current position
                space(side - i);
                Console.Write("*");
                if (i != 0)
                {
                    space(i + i - 1);
                    Console.Write("*");
                }
            }
            else
            {
                //Include last layer of top section
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            Console.Write("\n");
        }
        //This is include middle top layer
        for (i = 0; i < (side-1); ++i)
        {
            space(i + 4);
            Console.Write("*");
            space((side - 1) * 6 - (i + 3 + i));
            Console.Write("*\n");
        }
        //This is include middle bottom layer
        for (i = 1; i < (side); ++i)
        {
            if (i < side - 1)
            {
                space(side + 2 - i);
                Console.Write("*");
                space((side - 1) * 4 + (i * 2) - 1);
                Console.Write("*");
            }
            else
            {
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            Console.Write("\n");
        }
        //This is include bottom layer
        for (i = 1; i < side; ++i)
        {
            space(3 + (side - 1) * 2 + i);
            Console.Write("*");
            if (i + 1 != side)
            {
                space((side - 1) * 2 - (i) * 2 - 1);
                Console.Write("*");
            }
            Console.Write("\n");
        }
    }
    public static void Main(String[] args)
    {
        //Make object
        MyPattern obj = new MyPattern();
        //Test Case
        obj.print_star(3);
        obj.print_star(5);
        obj.print_star(7);
        obj.print_star(4);
    }
}

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
<?php
/*
Php Program 
Display 6 pointed star pattern
*/
class MyPattern
{
    //include space 
    public  function space($size)
    {
        for ($i = 0; $i < $size; ++$i)
        {
            echo " ";
        }
    }
    //include star 
    public  function star($size)
    {
        for ($i = 0; $i < $size; ++$i)
        {
            if ($i % 2 == 0)
            {
                echo "*";
            }
            else
            {
                echo " ";
            }
        }
    }
    //Function which is displaying a star shape of given side
    public  function print_star($side)
    {
        if ($side < 3)
        {
            return;
        }
        $i = 0;
        echo "\n Side Length : ". $side ."\n\n";
        //loop which is display the top layer of star pattern
        for ($i = 0; $i < ($side); ++$i)
        {
            if ($i < $side - 1)
            {
                //initial distance using space
                $this->space($side * 2);
                //This is initial space of current position
                $this->space($side - $i);
                echo "*";
                if ($i != 0)
                {
                    $this->space($i + $i - 1);
                    echo "*";
                }
            }
            else
            {
                //Include last layer of top section
                $this->space(3);
                $this->star($side * 2);
                $this->space(($side - 2) * 2);
                $this->star($side * 2);
            }
            echo "\n";
        }
        //This is include middle top layer
        for ($i = 0; $i < ($side-1); ++$i)
        {
            $this->space($i + 4);
            echo "*";
            $this->space(($side - 1) * 6 - ($i + 3 + $i));
            echo "*\n";
        }
        //This is include middle bottom layer
        for ($i = 1; $i < ($side); ++$i)
        {
            if ($i < $side - 1)
            {
                $this->space($side + 2 - $i);
                echo "*";
                $this->space(($side - 1) * 4 + ($i * 2) - 1);
                echo "*";
            }
            else
            {
                $this->space(3);
                $this->star($side * 2);
                $this->space(($side - 2) * 2);
                $this->star($side * 2);
            }
            echo "\n";
        }
        //This is include bottom layer
        for ($i = 1; $i < $side; ++$i)
        {
            $this->space(3 + ($side - 1) * 2 + $i);
            echo "*";
            if ($i + 1 != $side)
            {
                $this->space(($side - 1) * 2 - ($i) * 2 - 1);
                echo "*";
            }
            echo "\n";
        }
    }
}

function main()
{
    //Make object
    $obj = new MyPattern();
    //Test Case
    $obj->print_star(3);
    $obj->print_star(5);
    $obj->print_star(7);
    $obj->print_star(4);
}
main();

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
/*
Node Js Program 
Display 6 pointed star pattern
*/
class MyPattern
{
    //include space 
    space(size)
    {
        for (var i = 0; i < size; ++i)
        {
            process.stdout.write(" ");
        }
    }
    //include star 
    star(size)
    {
        for (var i = 0; i < size; ++i)
        {
            if (i % 2 == 0)
            {
                process.stdout.write("*");
            }
            else
            {
                process.stdout.write(" ");
            }
        }
    }
    //Function which is displaying a star shape of given side
    print_star(side)
    {
        if (side < 3)
        {
            return;
        }
        var i = 0;
        process.stdout.write("\n Side Length : " + side + "\n\n");
        //loop which is display the top layer of star pattern
        for (i = 0; i < (side); ++i)
        {
            if (i < side - 1)
            {
                //initial distance using space
                this.space(side * 2);
                //This is initial space of current position
                this.space(side - i);
                process.stdout.write("*");
                if (i != 0)
                {
                    this.space(i + i - 1);
                    process.stdout.write("*");
                }
            }
            else
            {
                //Include last layer of top section
                this.space(3);
                this.star(side * 2);
                this.space((side - 2) * 2);
                this.star(side * 2);
            }
            process.stdout.write("\n");
        }
        //This is include middle top layer
        for (i = 0; i < (side-1); ++i)
        {
            this.space(i + 4);
            process.stdout.write("*");
            this.space((side - 1) * 6 - (i + 3 + i));
            process.stdout.write("*\n");
        }
        //This is include middle bottom layer
        for (i = 1; i < (side); ++i)
        {
            if (i < side - 1)
            {
                this.space(side + 2 - i);
                process.stdout.write("*");
                this.space((side - 1) * 4 + (i * 2) - 1);
                process.stdout.write("*");
            }
            else
            {
                this.space(3);
                this.star(side * 2);
                this.space((side - 2) * 2);
                this.star(side * 2);
            }
            process.stdout.write("\n");
        }
        //This is include bottom layer
        for (i = 1; i < side; ++i)
        {
            this.space(3 + (side - 1) * 2 + i);
            process.stdout.write("*");
            if (i + 1 != side)
            {
                this.space((side - 1) * 2 - (i) * 2 - 1);
                process.stdout.write("*");
            }
            process.stdout.write("\n");
        }
    }
}

function main()
{
    //Make object
    var obj = new MyPattern();
    //Test Case
    obj.print_star(3);
    obj.print_star(5);
    obj.print_star(7);
    obj.print_star(4);
}
main();

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
# Python 3 Program 
# Display 6 pointed star pattern

class MyPattern :
    # include space 
    def space(self, size) :
        i = 0
        while (i < size) :
            print(" ", end = "")
            i += 1
        
    
    # include star 
    def star(self, size) :
        i = 0
        while (i < size) :
            if (i % 2 == 0) :
                print("*", end = "")
            else :
                print(" ", end = "")
            
            i += 1
        
    
    # Function which is displaying a star shape of given side
    def print_star(self, side) :
        if (side < 3) :
            return
        
        i = 0
        print("\n Side Length : ", side ,"\n\n", end = "")
        # loop which is display the top layer of star pattern
        while (i < (side)) :
            if (i < side - 1) :
                # initial distance using space
                self.space(side * 2)
                # This is initial space of current position
                self.space(side - i)
                print("*", end = "")
                if (i != 0) :
                    self.space(i + i - 1)
                    print("*", end = "")
                
            else :
                # Include last layer of top section
                self.space(3)
                self.star(side * 2)
                self.space((side - 2) * 2)
                self.star(side * 2)
            
            print("\n", end = "")
            i += 1
        
        i = 0
        # This is include middle top layer
        while (i < (side-1)) :
            self.space(i + 4)
            print("*", end = "")
            self.space((side - 1) * 6 - (i + 3 + i))
            print("*\n", end = "")
            i += 1
        
        i = 1
        # This is include middle bottom layer
        while (i < (side)) :
            if (i < side - 1) :
                self.space(side + 2 - i)
                print("*", end = "")
                self.space((side - 1) * 4 + (i * 2) - 1)
                print("*", end = "")
            else :
                self.space(3)
                self.star(side * 2)
                self.space((side - 2) * 2)
                self.star(side * 2)
            
            print("\n", end = "")
            i += 1
        
        i = 1
        # This is include bottom layer
        while (i < side) :
            self.space(3 + (side - 1) * 2 + i)
            print("*", end = "")
            if (i + 1 != side) :
                self.space((side - 1) * 2 - (i) * 2 - 1)
                print("*", end = "")
            
            print("\n", end = "")
            i += 1
        
    

def main() :
    # Make object
    obj = MyPattern()
    # Test Case
    obj.print_star(3)
    obj.print_star(5)
    obj.print_star(7)
    obj.print_star(4)

if __name__ == "__main__": main()

Output

 Side Length :  3

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

 Side Length :  5

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

 Side Length :  7

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

 Side Length :  4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
# Ruby Program 
# Display 6 pointed star pattern

class MyPattern

    # include space 
    def space(size)
    
        i = 0
        while (i < size)
        
            print(" ")
            i += 1
        end
    end
    # include star 
    def star(size)
    
        i = 0
        while (i < size)
        
            if (i % 2 == 0)
            
                print("*")
            else
            
                print(" ")
            end
            i += 1
        end
    end
    # Function which is displaying a star shape of given side
    def print_star(side)
    
        if (side < 3)
        
            return
        end
        i = 0
        print("\n Side Length : ", side ,"\n\n")
        # loop which is display the top layer of star pattern
        while (i < (side))
        
            if (i < side - 1)
            
                # initial distance using space
                self.space(side * 2)
                # This is initial space of current position
                self.space(side - i)
                print("*")
                if (i != 0)
                
                    self.space(i + i - 1)
                    print("*")
                end
            else
            
                # Include last layer of top section
                self.space(3)
                self.star(side * 2)
                self.space((side - 2) * 2)
                self.star(side * 2)
            end
            print("\n")
            i += 1
        end
        i = 0
        # This is include middle top layer
        while (i < (side-1))
        
            self.space(i + 4)
            print("*")
            self.space((side - 1) * 6 - (i + 3 + i))
            print("*\n")
            i += 1
        end
        i = 1
        # This is include middle bottom layer
        while (i < (side))
        
            if (i < side - 1)
            
                self.space(side + 2 - i)
                print("*")
                self.space((side - 1) * 4 + (i * 2) - 1)
                print("*")
            else
            
                self.space(3)
                self.star(side * 2)
                self.space((side - 2) * 2)
                self.star(side * 2)
            end
            print("\n")
            i += 1
        end
        i = 1
        # This is include bottom layer
        while (i < side)
        
            self.space(3 + (side - 1) * 2 + i)
            print("*")
            if (i + 1 != side)
            
                self.space((side - 1) * 2 - (i) * 2 - 1)
                print("*")
            end
            print("\n")
            i += 1
        end
    end
end
def main()

    # Make object
    obj = MyPattern.new()
    # Test Case
    obj.print_star(3)
    obj.print_star(5)
    obj.print_star(7)
    obj.print_star(4)
end
main()

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * * 
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * * 
          *   *
           * *
            *
/*
Scala Program 
Display 6 pointed star pattern
*/
class MyPattern
{
    //include space 
    def space(size: Int): Unit = {
        var i: Int = 0;
        while (i < size)
        {
            print(" ");
            i += 1;
        }
    }
    //include star 
    def star(size: Int): Unit = {
        var i: Int = 0;
        while (i < size)
        {
            if (i % 2 == 0)
            {
                print("*");
            }
            else
            {
                print(" ");
            }
            i += 1;
        }
    }
    //Function which is displaying a star shape of given side
    def print_star(side: Int): Unit = {
        if (side < 3)
        {
            return;
        }
        var i: Int = 0;
        print("\n Side Length : " + side + "\n\n");
        //loop which is display the top layer of star pattern
        while (i < (side))
        {
            if (i < side - 1)
            {
                //initial distance using space
                space(side * 2);
                //This is initial space of current position
                space(side - i);
                print("*");
                if (i != 0)
                {
                    space(i + i - 1);
                    print("*");
                }
            }
            else
            {
                //Include last layer of top section
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            print("\n");
            i += 1;
        }
        i = 0;
        //This is include middle top layer
        while (i < (side-1))
        {
            space(i + 4);
            print("*");
            space((side - 1) * 6 - (i + 3 + i));
            print("*\n");
            i += 1;
        }
        i = 1;
        //This is include middle bottom layer
        while (i < (side))
        {
            if (i < side - 1)
            {
                space(side + 2 - i);
                print("*");
                space((side - 1) * 4 + (i * 2) - 1);
                print("*");
            }
            else
            {
                space(3);
                star(side * 2);
                space((side - 2) * 2);
                star(side * 2);
            }
            print("\n");
            i += 1;
        }
        i = 1;
        //This is include bottom layer
        while (i < side)
        {
            space(3 + (side - 1) * 2 + i);
            print("*");
            if (i + 1 != side)
            {
                space((side - 1) * 2 - (i) * 2 - 1);
                print("*");
            }
            print("\n");
            i += 1;
        }
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        //Make object
        var obj: MyPattern = new MyPattern();
        //Test Case
        obj.print_star(3);
        obj.print_star(5);
        obj.print_star(7);
        obj.print_star(4);
    }
}

Output

 Side Length : 3

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

 Side Length : 5

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

 Side Length : 7

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

 Side Length : 4

            *
           * *
          *   *
   * * * *     * * * *
    *               *
     *             *
      *           *
     *             *
    *               *
   * * * *     * * * *
          *   *
           * *
            *
/*
Swift Program 
Display 6 pointed star pattern
*/
class MyPattern
{
    //include space 
    func space(_ size: Int)
    {
        var i: Int = 0;
        while (i < size)
        {
            print(" ", terminator: "");
            i += 1;
        }
    }
    //include star 
    func star(_ size: Int)
    {
        var i: Int = 0;
        while (i < size)
        {
            if (i % 2 == 0)
            {
                print("*", terminator: "");
            }
            else
            {
                print(" ", terminator: "");
            }
            i += 1;
        }
    }
    //Function which is displaying a star shape of given side
    func print_star(_ side: Int)
    {
        if (side < 3)
        {
            return;
        }
        var i: Int = 0;
        print("\n Side Length : ", side ,"\n\n", terminator: "");
        //loop which is display the top layer of star pattern
        while (i < (side))
        {
            if (i < side - 1)
            {
                //initial distance using space
                self.space(side * 2);
                //This is initial space of current position
                self.space(side - i);
                print("*", terminator: "");
                if (i != 0)
                {
                    self.space(i + i - 1);
                    print("*", terminator: "");
                }
            }
            else
            {
                //Include last layer of top section
                self.space(3);
                self.star(side * 2);
                self.space((side - 2) * 2);
                self.star(side * 2);
            }
            print("\n", terminator: "");
            i += 1;
        }
        i = 0;
        //This is include middle top layer
        while (i < (side-1))
        {
            self.space(i + 4);
            print("*", terminator: "");
            self.space((side - 1) * 6 - (i + 3 + i));
            print("*\n", terminator: "");
            i += 1;
        }
        i = 1;
        //This is include middle bottom layer
        while (i < (side))
        {
            if (i < side - 1)
            {
                self.space(side + 2 - i);
                print("*", terminator: "");
                self.space((side - 1) * 4 + (i * 2) - 1);
                print("*", terminator: "");
            }
            else
            {
                self.space(3);
                self.star(side * 2);
                self.space((side - 2) * 2);
                self.star(side * 2);
            }
            print("\n", terminator: "");
            i += 1;
        }
        i = 1;
        //This is include bottom layer
        while (i < side)
        {
            self.space(3 + (side - 1) * 2 + i);
            print("*", terminator: "");
            if (i + 1 != side)
            {
                self.space((side - 1) * 2 - (i) * 2 - 1);
                print("*", terminator: "");
            }
            print("\n", terminator: "");
            i += 1;
        }
    }
}
func main()
{
    //Make object
    let obj: MyPattern = MyPattern();
    //Test Case
    obj.print_star(3);
    obj.print_star(5);
    obj.print_star(7);
    obj.print_star(4);
}
main();

Output

 Side Length :  3

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

 Side Length :  5

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

 Side Length :  7

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

 Side Length :  4

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


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