Posted on by Kalkicode
Code Pattern

Print star pattern

In this article, we will discuss how to print a star pattern using C programming language. The star pattern is a common exercise in programming that helps improve your logical thinking and problem-solving skills. We will explain the problem statement, provide a suitable example, present the algorithm and pseudocode, and discuss the output and time complexity of the code.

Problem Statement

The task is to write a program that prints a star pattern based on a given side length. The program should take an integer input representing the side length of the star and display the corresponding star pattern.

Print interactive star pattern

Example:

Let's say we have a side length of 5. The star pattern would look like this:

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

Algorithm and Pseudocode

To solve this problem, we can use a combination of loops and conditional statements. Here is the algorithm to print the star pattern:

  1. Define a function called "space" that takes an integer argument "size" and prints that many spaces.
  2. Define a function called "print_star" that takes an integer argument "side" representing the side length of the star.
  3. Inside the "print_star" function, check if the "side" is less than 4. If it is, return from the function as a star pattern cannot be formed.
  4. Calculate the "side_corner" value as (side * 2) - 3. This represents the number of spaces between the corners of the star.
  5. Calculate the "top" value as side / 4. If "top" is less than 2, set it to 2. This is the number of layers in the top section of the star.
  6. Print the top layer of the star using nested loops. The outer loop iterates from 0 to "top", and the inner loop iterates from 0 to "side * 2".
  7. Inside the inner loop, check if the current position is at the top layer. If it is, print a star and increment "j" to skip the next position.
  8. Otherwise, check if the current position is at the corners of the star. If it is, print a star. Otherwise, print a space.
  9. After printing the top layer, print the middle and bottom layers of the star using nested loops.
  10. Finally, in the "main" function, call the "print_star" function with different test cases.

Here is the pseudocode representation of the algorithm:


function space(size):
    for i = 0 to size:
        print " "

function print_star(side):
    if side < 4:
        return

    side_corner = (side * 2) - 3
    top = side / 4
    if top < 2:
        top = 2

    for i = 0 to top:
        space(5)
        for j = 0 to side * 2:
            if i == top:
                print "* "
                j = j + 1
            else if j == side - 1 - i or j == side - 1 + i:
                print "*"
            else:
                print " "

    for i = 1 to side:
        space(5)
        for j = 0 to side * 2:
            if (side - 1) + i == (side - 1) * 2 - top:
                print "* "
                j = j + 1
            else if 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 "*"
            else:
                print " "

main:
    print_star(11)
    print_star(7)
    print_star(14)
    print_star(9)

Code Solution

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

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

The output shows the star pattern for different test cases. Each asterisk (*) represents a star, and the spaces are denoted by blanks. The pattern is symmetric around the center and forms a star shape based on the given side length.

Time Complexity

The time complexity of the code is primarily determined by the nested loops used to print the star pattern. Let's analyze the time complexity of the "print_star" function:

  • The first set of nested loops iterates from 0 to "top" and from 0 to "side * 2". The number of iterations is approximately proportional to "top * side".
  • The second set of nested loops iterates from 1 to "side" and from 0 to "side * 2". The number of iterations is approximately proportional to "side * side".

Therefore, the overall time complexity can be approximated as O(side * side), where "side" is the given side length of the star. The space complexity of the code is O(1) as it uses a constant amount of additional space.

Comment

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

New Comment