Print star pattern

Print interactive star pattern

Here given code implementation process.

// C Program
// Print star pattern 
#include <stdio.h>

//include space 
void space(int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf(" ");
	}
}
//Function which is displaying a star shape of given side
void print_star(int side)
{
	if (side < 4)
	{
		return;
	}
	printf("Side : %d\n\n", side);
	int side_corner = (side * 2) - 3;
	int top = side / 4;
	if (top < 2)
	{
		top = 2;
	}
	//Display top layer
	for (int i = 0; i <= top; i++)
	{
		//initial space
		space(5);
		for (int j = 0; j < side * 2; ++j)
		{
			if (i == top)
			{
				//When Displayed of horizontal row of star
				printf("* ");
				j++;
			}
			else if (j == side - 1 - i || j == side - 1 + i)
			{
				printf("*");
			}
			else
			{
				//include single space
				printf(" ");
			}
		}
		printf("\n");
	}
	//Printed middle and bottom layers
	for (int i = 1; i < side; i++)
	{
		//initial space
		space(5);
		for (int j = 0; j < side * 2; ++j)
		{
			if ((side - 1) + i == (side - 1) * 2 - top)
			{
				//When Displayed of horizontal row of star
				printf("* ");
				j++;
			}
			else if (j == i 
                     || (i == side + top) 
                     || j == (side - 1) * 2 - i 
                     || (j + top <= side * 2) && j == side + (top - 1) + i 
                     || j == side - top - 1 - i)
			{
				printf("*");
			}
			else
			{
				//include single space
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("\n");
}
int main()
{
	//Test Case
	print_star(11);
	print_star(7);
	print_star(14);
	print_star(9);
	return 0;
}

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
/*
  Java Program
  Print star pattern
*/
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" ");
		}
	}
	//Function which is displaying a star shape of given side
	public void print_star(int side)
	{
		if (side < 4)
		{
			return;
		}
		System.out.print("Side : " + side + "\n\n");
		int side_corner = (side * 2) - 3;
		int top = side / 4;
		if (top < 2)
		{
			top = 2;
		}
		//Display top layer
		for (int i = 0; i <= top; i++)
		{
			//initial space
			space(5);
			for (int j = 0; j < side * 2; ++j)
			{
				if (i == top)
				{
					//When Displayed of horizontal row of star
					System.out.print("* ");
					j++;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					System.out.print("*");
				}
				else
				{
					//include single space
					System.out.print(" ");
				}
			}
			System.out.print("\n");
		}
		//Printed middle and bottom layers
		for (int i = 1; i < side; i++)
		{
			//initial space
			space(5);
			for (int j = 0; j < side * 2; ++j)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					//When Displayed of horizontal row of star
					System.out.print("* ");
					j++;
				}
				else if (j == i || (i == side + top) || j == (side - 1) * 2 - i || (j + top <= side * 2) && j == side + (top - 1) + i || j == side - top - 1 - i)
				{
					System.out.print("*");
				}
				else
				{
					//include single space
					System.out.print(" ");
				}
			}
			System.out.print("\n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Case
		obj.print_star(11);
		obj.print_star(7);
		obj.print_star(14);
		obj.print_star(9);
	}
}

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
/*
  C++ Program
  Print star pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public:
		//include space 
		void space(int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " ";
			}
		}
	//Function which is displaying a star shape of given side
	void print_star(int side)
	{
		if (side < 4)
		{
			return;
		}
		cout << "Side : " << side << "\n\n";
		int side_corner = (side * 2) - 3;
		int top = side / 4;
		if (top < 2)
		{
			top = 2;
		}
		//Display top layer
		for (int i = 0; i <= top; i++)
		{
			//initial space
			this->space(5);
			for (int j = 0; j < side * 2; ++j)
			{
				if (i == top)
				{
					cout << "* ";
					j++;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
			}
			cout << "\n";
		}
		//Printed middle and bottom layers
		for (int i = 1; i < side; i++)
		{
			//initial space
			this->space(5);
			for (int j = 0; j < side * 2; ++j)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					cout << "* ";
					j++;
				}
				else if (j == i 
                         || (i == side + top) 
                         || j == (side - 1) * 2 - i 
                         || (j + top <= side * 2) && j == side + (top - 1) + i 
                         || j == side - top - 1 - i)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main()
{
	MyPattern obj ;
	//Test Case
	obj.print_star(11);
	obj.print_star(7);
	obj.print_star(14);
	obj.print_star(9);
	return 0;
}

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
/*
  C# Program
  Print star pattern
*/
using System;
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write(" ");
		}
	}
	//Function which is displaying a star shape of given side
	public void print_star(int side)
	{
		if (side < 4)
		{
			return;
		}
		Console.Write("Side : " + side + "\n\n");
	
		int top = side / 4;
		if (top < 2)
		{
			top = 2;
		}
		//Display top layer
		for (int i = 0; i <= top; i++)
		{
			//initial space
			space(5);
			for (int j = 0; j < side * 2; j++)
			{
				if (i == top)
				{
					Console.Write("* ");
					j++;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					Console.Write("*");
				}
				else
				{
					Console.Write(" ");
				}
			}
			Console.Write("\n");
		}
		//Printed middle and bottom layers
		for (int i = 1; i < side; i++)
		{
			//initial space
			space(5);
			for (int j = 0; j < side * 2; j++)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					Console.Write("* ");
					j++;
				}
				else if (j == i 
                         || (i == side + top) 
                         || j == (side - 1) * 2 - i 
                         || (j + top <= side * 2) && j == side + (top - 1) + i 
                         || j == side - top - 1 - i)
				{
					Console.Write("*");
				}
				else
				{
					Console.Write(" ");
				}
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Case
		obj.print_star(11);
		obj.print_star(7);
		obj.print_star(14);
		obj.print_star(9);
	}
}

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
<?php
/*
  Php Program
  Print star pattern
*/
class MyPattern
{
	//include space 
	function space($size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ";
		}
	}
	//Function which is displaying a star shape of given side
	function print_star($side)
	{
		if ($side < 4)
		{
			return;
		}
		echo "Side : ". $side ."\n\n";
		$top = intval($side / 4);
		if ($top < 2)
		{
			$top = 2;
		}
		//Display top layer
		for ($i = 0; $i <= $top; $i++)
		{
			//initial space
			$this->space(5);
			for ($j = 0; $j < $side * 2; ++$j)
			{
				if ($i == $top)
				{
					echo "* ";
					$j++;
				}
				else if ($j == $side - 1 - $i || $j == $side - 1 + $i)
				{
					echo "*";
				}
				else
				{
					echo " ";
				}
			}
			echo "\n";
		}
		//Printed middle and bottom layers
		for ($i = 1; $i < $side; $i++)
		{
			//initial space
			$this->space(5);
			for ($j = 0; $j < $side * 2; ++$j)
			{
				if (($side - 1) + $i == ($side - 1) * 2 - $top)
				{
					echo "* ";
					$j++;
				}
				else if ($j == $i 
                         || ($i == $side + $top) 
                         || $j == ($side - 1) * 2 - $i 
                         || ($j + $top <= $side * 2) && $j == $side + ($top - 1) + $i 
                         || $j == $side - $top - 1 - $i)
				{
					echo "*";
				}
				else
				{
					echo " ";
				}
			}
			echo "\n";
		}
		echo "\n";
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Case
	$obj->print_star(11);
	$obj->print_star(7);
	$obj->print_star(14);
	$obj->print_star(9);
}
main();

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
/*
  Node Js Program
  Print star pattern
*/
class MyPattern
{
	//include space 
	space(size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" ");
		}
	}
	//Function which is displaying a star shape of given side
	print_star(side)
	{
		if (side < 4)
		{
			return;
		}
		process.stdout.write("Side : " + side + "\n\n");
		var top = parseInt(side / 4);
		if (top < 2)
		{
			top = 2;
		}
		//Display top layer
		for (var i = 0; i <= top; i++)
		{
			//initial space
			this.space(5);
			for (var j = 0; j < side * 2; ++j)
			{
				if (i == top)
				{
					process.stdout.write("* ");
					j++;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					process.stdout.write("*");
				}
				else
				{
					process.stdout.write(" ");
				}
			}
			process.stdout.write("\n");
		}
		//Printed middle and bottom layers
		for (var i = 1; i < side; i++)
		{
			//initial space
			this.space(5);
			for (var j = 0; j < side * 2; ++j)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					process.stdout.write("* ");
					j++;
				}
				else if (j == i 
                         || (i == side + top) 
                         || j == (side - 1) * 2 - i 
                         || (j + top <= side * 2) && j == side + (top - 1) + i 
                         || j == side - top - 1 - i)
				{
					process.stdout.write("*");
				}
				else
				{
					process.stdout.write(" ");
				}
			}
			process.stdout.write("\n");
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var obj = new MyPattern();
	//Test Case
	obj.print_star(11);
	obj.print_star(7);
	obj.print_star(14);
	obj.print_star(9);
}
main();

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
#   Python 3 Program
#   Print star pattern

class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	# Function which is displaying a star shape of given side
	def print_star(self, side) :
		if (side < 4) :
			return
		
		print("Side : ", side ,"\n\n", end = "")
		top = int(side / 4)
		if (top < 2) :
			top = 2
		
		i = 0
		j = 0
		# Display top layer
		while (i <= top) :
			# initial space
			self.space(5)
			j = 0
			while (j < side * 2) :
				if (i == top) :
					print("* ", end = "")
					j += 1
				
				elif(j == side - 1 - i or j == side - 1 + i) :
					print("*", end = "")
				else :
					print(" ", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
		# Printed middle and bottom layers
		i = 1
		while (i < side) :
			# initial space
			self.space(5)
			j = 0
			while (j < side * 2) :
				if ((side - 1) + i == (side - 1) * 2 - top) :
					print("* ", end = "")
					j += 1
				
				elif(j == i or(i == side + top) or j == (side - 1) * 2 - i or(j + top <= side * 2) and j == side + (top - 1) + i or j == side - top - 1 - i) :
					print("*", end = "")
				else :
					print(" ", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyPattern()
	# Test Case
	obj.print_star(11)
	obj.print_star(7)
	obj.print_star(14)
	obj.print_star(9)

if __name__ == "__main__": main()

Output

Side :  11

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

Side :  7

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

Side :  14

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

Side :  9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
#   Ruby Program
#   Print star pattern

class MyPattern

	# include space 
	def space(size)
	
		i = 0
		while (i < size)
		
			print(" ")
			i += 1
		end
	end
	# Function which is displaying a star shape of given side
	def print_star(side)
	
		if (side < 4)
		
			return
		end
		print("Side : ", side ,"\n\n")
		top = side / 4
		if (top < 2)
		
			top = 2
		end
		i = 0
		j = 0
		# Display top layer
		while (i <= top)
		
			# initial space
			self.space(5)
			j = 0
			while (j < side * 2)
			
				if (i == top)
				
					# When Displayed of horizontal row of star
					print("* ")
					j += 1
				elsif(j == side - 1 - i || j == side - 1 + i)
				
					print("*")
				else
				
					# include single space
					print(" ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
		# Printed middle and bottom layers
		i = 1
		while (i < side)
		
			# initial space
			self.space(5)
			j = 0
			while (j < side * 2)
			
				if ((side - 1) + i == (side - 1) * 2 - top)
				
					# When Displayed of horizontal row of star
					print("* ")
					j += 1
				elsif(j == i || (i == side + top) || j == (side - 1) * 2 - i || (j + top <= side * 2) && j == side + (top - 1) + i || j == side - top - 1 - i)
				
					print("*")
				else
				
					# include single space
					print(" ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main()

	obj = MyPattern.new()
	# Test Case
	obj.print_star(11)
	obj.print_star(7)
	obj.print_star(14)
	obj.print_star(9)
end
main()

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

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

/*
  Scala Program
  Print star pattern
*/
class MyPattern
{
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" ");
			i += 1;
		}
	}
	//Function which is displaying a star shape of given side
	def print_star(side: Int): Unit = {
		if (side < 4)
		{
			return;
		}
		print("Side : " + side + "\n\n");
		var top: Int = (side / 4).toInt;
		if (top < 2)
		{
			top = 2;
		}
		var i: Int = 0;
		var j: Int = 0;
		//Display top layer
		while (i <= top)
		{
			//initial space
			space(5);
			j = 0;
			while (j < side * 2)
			{
				if (i == top)
				{
					//When Displayed of horizontal row of star
					print("* ");
					j += 1;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					print("*");
				}
				else
				{
					//include single space
					print(" ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
		//Printed middle and bottom layers
		i = 1;
		while (i < side)
		{
			//initial space
			space(5);
			j = 0;
			while (j < side * 2)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					//When Displayed of horizontal row of star
					print("* ");
					j += 1;
				}
				else if (j == i || (i == side + top) || j == (side - 1) * 2 - i || (j + top <= side * 2) && j == side + (top - 1) + i || j == side - top - 1 - i)
				{
					print("*");
				}
				else
				{
					//include single space
					print(" ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Case
		obj.print_star(11);
		obj.print_star(7);
		obj.print_star(14);
		obj.print_star(9);
	}
}

Output

Side : 11

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

Side : 7

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

Side : 14

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

Side : 9

             *
            * *
     * * * * * * * * *
      *   *     *   *
       * *       * *
        *         *
       * *       * *
      *   *     *   *
     * * * * * * * * *
            * *
             *
/*
  Swift Program
  Print star pattern
*/
class MyPattern
{
	//include space 
	func space(_ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	//Function which is displaying a star shape of given side
	func print_star(_ side: Int)
	{
		if (side < 4)
		{
			return;
		}
		print("Side : ", side ,"\n\n", terminator: "");
		var top: Int = side / 4;
		if (top < 2)
		{
			top = 2;
		}
		var i: Int = 0;
		var j: Int = 0;
		//Display top layer
		while (i <= top)
		{
			//initial space
			self.space(5);
			j = 0;
			while (j < side * 2)
			{
				if (i == top)
				{
					print("* ", terminator: "");
					j += 1;
				}
				else if (j == side - 1 - i || j == side - 1 + i)
				{
					print("*", terminator: "");
				}
				else
				{
					print(" ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		//Printed middle and bottom layers
		i = 1;
		while (i < side)
		{
			//initial space
			self.space(5);
			j = 0;
			while (j < side * 2)
			{
				if ((side - 1) + i == (side - 1) * 2 - top)
				{
					print("* ", terminator: "");
					j += 1;
				}
				else if (j == i 
                         || (i == side + top) 
                         || j == (side - 1) * 2 - i 
                         || (j + top <= side * 2) && j == side + (top - 1) + i 
                         || j == side - top - 1 - i)
				{
					print("*", terminator: "");
				}
				else
				{
					print(" ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Case
	obj.print_star(11);
	obj.print_star(7);
	obj.print_star(14);
	obj.print_star(9);
}
main();

Output

Side :  11

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

Side :  7

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

Side :  14

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

Side :  9

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

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