Print cube star pattern

Here given code implementation process.

//C Program 
//Display cube star pattern
#include <stdio.h>
 //include space 
void space(int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf(" ");
	}
}
void print_cube(int width)
{
	if (width % 2 == 0 || width <= 2)
	{
		return;
	}
	printf("\n\nWidth : %d\n\n", width);
	//Top Layer
	/*
	  Print like this

	          *
	        *   *
	      *       *
	    *           *
	  *               *

	*/
	for (int i = 0; i <= width / 2; i++)
	{
		space((width - 1) * 2 - (i) * 4);
		printf("*");
		space(i * 8 - 1);
		if (i != 0)
		{
			printf("*");
		}
		printf("\n");
	}
	//middle layer
	/*
	  Print like this

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

	*/
	for (int i = 1; i <= width / 2; i++)
	{
		printf("*");
		space(i * 4 - 1);
		printf("*");
		space((width - 1) * 4 - 1 - i * 8);
		if (i < width / 2)
		{
			printf("*");
		}
		space(i * 4 - 1);
		printf("*");
		printf("\n");
	}
	/*
	  Print like this

	  *       *       *
	  *       *       *

	*/
	for (int i = 1; i <= width / 2; i++)
	{
		printf("*");
		//middle elements
		space((width - 1) * 2 - 1);
		printf("*");
		//bottom right width elements
		space((width - 1) * 2 - 1);
		printf("*");
		printf("\n");
	}
	//Bottom Layer
	/*
	  Print like this

	  *     *     *
	    *   *   *
	      * * *
	        *
	*/
	for (int i = 1; i <= width / 2; i++)
	{
		//bottom left width element
		space(i * 4);
		printf("*");
		if (i != width / 2)
		{
			//middle elements
			space((width - 1) * 2 - i * 4 - 1);
			printf("*");
			//bottom right width elements
			space((width - 1) * 2 - i * 4 - 1);
			printf("*");
		}
		printf("\n");
	}
}
int main()
{
	//Simple test
	print_cube(5);
	print_cube(7);
	print_cube(11);
	print_cube(9);
	return 0;
}

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
/*
  Java Program
  Display cube star pattern
*/
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" ");
		}
	}
	public void print_cube(int width)
	{
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		System.out.print("\n\nWidth : " + width + "\n\n");
		for (int i = 0; i <= width / 2; i++)
		{
			space((width - 1) * 2 - (i) * 4);
			System.out.print("*");
			space(i * 8 - 1);
			if (i != 0)
			{
				System.out.print("*");
			}
			System.out.print("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			System.out.print("*");
			space(i * 4 - 1);
			System.out.print("*");
			space((width - 1) * 4 - 1 - i * 8);
			if (i < width / 2)
			{
				System.out.print("*");
			}
			space(i * 4 - 1);
			System.out.print("*");
			System.out.print("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			System.out.print("*");
			//middle elements
			space((width - 1) * 2 - 1);
			System.out.print("*");
			//bottom right width elements
			space((width - 1) * 2 - 1);
			System.out.print("*");
			System.out.print("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			//bottom left width element
			space(i * 4);
			System.out.print("*");
			if (i != width / 2)
			{
				//middle elements
				space((width - 1) * 2 - i * 4 - 1);
				System.out.print("*");
				//bottom right width elements
				space((width - 1) * 2 - i * 4 - 1);
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.print_cube(5);
		obj.print_cube(7);
		obj.print_cube(11);
		obj.print_cube(9);
	}
}

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

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

using namespace std;
class MyPattern
{
	public:
		//include space 
		void space(int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " ";
			}
		}
	void print_cube(int width)
	{
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		cout << "\n\nWidth : " << width << "\n\n";
		for (int i = 0; i <= width / 2; i++)
		{
			this->space((width - 1) * 2 - (i) * 4);
			cout << "*";
			this->space(i * 8 - 1);
			if (i != 0)
			{
				cout << "*";
			}
			cout << "\n";
		}
		for (int i = 1; i <= width / 2; i++)
		{
			cout << "*";
			this->space(i * 4 - 1);
			cout << "*";
			this->space((width - 1) * 4 - 1 - i * 8);
			if (i < width / 2)
			{
				cout << "*";
			}
			this->space(i * 4 - 1);
			cout << "*";
			cout << "\n";
		}
		for (int i = 1; i <= width / 2; i++)
		{
			cout << "*";
			//middle elements
			this->space((width - 1) * 2 - 1);
			cout << "*";
			//bottom right width elements
			this->space((width - 1) * 2 - 1);
			cout << "*";
			cout << "\n";
		}
		for (int i = 1; i <= width / 2; i++)
		{
			//bottom left width element
			this->space(i * 4);
			cout << "*";
			if (i != width / 2)
			{
				//middle elements
				this->space((width - 1) * 2 - i * 4 - 1);
				cout << "*";
				//bottom right width elements
				this->space((width - 1) * 2 - i * 4 - 1);
				cout << "*";
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj ;
	//Test Cases
	obj.print_cube(5);
	obj.print_cube(7);
	obj.print_cube(11);
	obj.print_cube(9);
	return 0;
}

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
/*
  C# Program
  Display cube star pattern
*/
using System;
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write(" ");
		}
	}
	public void print_cube(int width)
	{
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		Console.Write("\n\nWidth : " + width + "\n\n");
		for (int i = 0; i <= width / 2; i++)
		{
			space((width - 1) * 2 - (i) * 4);
			Console.Write("*");
			space(i * 8 - 1);
			if (i != 0)
			{
				Console.Write("*");
			}
			Console.Write("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			Console.Write("*");
			space(i * 4 - 1);
			Console.Write("*");
			space((width - 1) * 4 - 1 - i * 8);
			if (i < width / 2)
			{
				Console.Write("*");
			}
			space(i * 4 - 1);
			Console.Write("*");
			Console.Write("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			Console.Write("*");
			//middle elements
			space((width - 1) * 2 - 1);
			Console.Write("*");
			//bottom right width elements
			space((width - 1) * 2 - 1);
			Console.Write("*");
			Console.Write("\n");
		}
		for (int i = 1; i <= width / 2; i++)
		{
			//bottom left width element
			space(i * 4);
			Console.Write("*");
			if (i != width / 2)
			{
				//middle elements
				space((width - 1) * 2 - i * 4 - 1);
				Console.Write("*");
				//bottom right width elements
				space((width - 1) * 2 - i * 4 - 1);
				Console.Write("*");
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.print_cube(5);
		obj.print_cube(7);
		obj.print_cube(11);
		obj.print_cube(9);
	}
}

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
<?php
/*
  Php Program
  Display cube star pattern
*/
class MyPattern
{
	//include space 
	function space($size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ";
		}
	}

	function print_cube($width)
	{
		if ($width % 2 == 0 || $width <= 2)
		{
			return;
		}
		echo "\n\nWidth : ". $width ."\n\n";
		for ($i = 0; $i <= intval($width / 2); $i++)
		{
			$this->space(($width - 1) * 2 - ($i) * 4);
			echo "*";
			$this->space($i * 8 - 1);
			if ($i != 0)
			{
				echo "*";
			}
			echo "\n";
		}
		for ($i = 1; $i <= intval($width / 2); $i++)
		{
			echo "*";
			$this->space($i * 4 - 1);
			echo "*";
			$this->space(($width - 1) * 4 - 1 - $i * 8);
			if ($i < intval($width / 2))
			{
				echo "*";
			}
			$this->space($i * 4 - 1);
			echo "*";
			echo "\n";
		}
		for ($i = 1; $i <= intval($width / 2); $i++)
		{
			echo "*";
			//middle elements
			$this->space(($width - 1) * 2 - 1);
			echo "*";
			//bottom right width elements
			$this->space(($width - 1) * 2 - 1);
			echo "*";
			echo "\n";
		}
		for ($i = 1; $i <= intval($width / 2); $i++)
		{
			//bottom left width element
			$this->space($i * 4);
			echo "*";
			if ($i != intval($width / 2))
			{
				//middle elements
				$this->space(($width - 1) * 2 - $i * 4 - 1);
				echo "*";
				//bottom right width elements
				$this->space(($width - 1) * 2 - $i * 4 - 1);
				echo "*";
			}
			echo "\n";
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->print_cube(5);
	$obj->print_cube(7);
	$obj->print_cube(11);
	$obj->print_cube(9);
}
main();

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
/*
  Node Js Program
  Display cube star pattern
*/
class MyPattern
{
	//include space 
	space(size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" ");
		}
	}
	print_cube(width)
	{
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		process.stdout.write("\n\nWidth : " + width + "\n\n");
		for (var i = 0; i <= parseInt(width / 2); i++)
		{
			this.space((width - 1) * 2 - (i) * 4);
			process.stdout.write("*");
			this.space(i * 8 - 1);
			if (i != 0)
			{
				process.stdout.write("*");
			}
			process.stdout.write("\n");
		}
		for (var i = 1; i <= parseInt(width / 2); i++)
		{
			process.stdout.write("*");
			this.space(i * 4 - 1);
			process.stdout.write("*");
			this.space((width - 1) * 4 - 1 - i * 8);
			if (i < parseInt(width / 2))
			{
				process.stdout.write("*");
			}
			this.space(i * 4 - 1);
			process.stdout.write("*");
			process.stdout.write("\n");
		}
		for (var i = 1; i <= parseInt(width / 2); i++)
		{
			process.stdout.write("*");
			//middle elements
			this.space((width - 1) * 2 - 1);
			process.stdout.write("*");
			//bottom right width elements
			this.space((width - 1) * 2 - 1);
			process.stdout.write("*");
			process.stdout.write("\n");
		}
		for (var i = 1; i <= parseInt(width / 2); i++)
		{
			//bottom left width element
			this.space(i * 4);
			process.stdout.write("*");
			if (i != parseInt(width / 2))
			{
				//middle elements
				this.space((width - 1) * 2 - i * 4 - 1);
				process.stdout.write("*");
				//bottom right width elements
				this.space((width - 1) * 2 - i * 4 - 1);
				process.stdout.write("*");
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var obj = new MyPattern();
	//Test Cases
	obj.print_cube(5);
	obj.print_cube(7);
	obj.print_cube(11);
	obj.print_cube(9);
}
main();

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
#   Python 3 Program
#   Display cube star pattern

class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	def print_cube(self, width) :
		if (width % 2 == 0 or width <= 2) :
			return
		
		print("\n\nWidth : ", width ,"\n\n", end = "")
		i = 0
		while (i <= int(width / 2)) :
			self.space((width - 1) * 2 - (i) * 4)
			print("*", end = "")
			self.space(i * 8 - 1)
			if (i != 0) :
				print("*", end = "")
			
			print("\n", end = "")
			i += 1
		
		i = 1
		while (i <= int(width / 2)) :
			print("*", end = "")
			self.space(i * 4 - 1)
			print("*", end = "")
			self.space((width - 1) * 4 - 1 - i * 8)
			if (i < int(width / 2)) :
				print("*", end = "")
			
			self.space(i * 4 - 1)
			print("*", end = "")
			print("\n", end = "")
			i += 1
		
		i = 1
		while (i <= int(width / 2)) :
			print("*", end = "")
			# middle elements
			self.space((width - 1) * 2 - 1)
			print("*", end = "")
			# bottom right width elements
			self.space((width - 1) * 2 - 1)
			print("*", end = "")
			print("\n", end = "")
			i += 1
		
		i = 1
		while (i <= int(width / 2)) :
			# bottom left width element
			self.space(i * 4)
			print("*", end = "")
			if (i != int(width / 2)) :
				# middle elements
				self.space((width - 1) * 2 - i * 4 - 1)
				print("*", end = "")
				# bottom right width elements
				self.space((width - 1) * 2 - i * 4 - 1)
				print("*", end = "")
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.print_cube(5)
	obj.print_cube(7)
	obj.print_cube(11)
	obj.print_cube(9)

if __name__ == "__main__": main()

Output

Width :  5

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


Width :  7

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


Width :  11

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


Width :  9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
#   Ruby Program
#   Display cube star pattern

class MyPattern

	# include space 
	def space(size)
	
		i = 0
		while (i < size)
		
			print(" ")
			i += 1
		end
	end
	def print_cube(width)
	
		if (width % 2 == 0 || width <= 2)
		
			return
		end
		print("\n\nWidth : ", width ,"\n\n")
		i = 0
		while (i <= width / 2)
		
			self.space((width - 1) * 2 - (i) * 4)
			print("*")
			self.space(i * 8 - 1)
			if (i != 0)
			
				print("*")
			end
			print("\n")
			i += 1
		end
		i = 1
		while (i <= width / 2)
		
			print("*")
			self.space(i * 4 - 1)
			print("*")
			self.space((width - 1) * 4 - 1 - i * 8)
			if (i < width / 2)
			
				print("*")
			end
			self.space(i * 4 - 1)
			print("*")
			print("\n")
			i += 1
		end
		i = 1
		while (i <= width / 2)
		
			print("*")
			# middle elements
			self.space((width - 1) * 2 - 1)
			print("*")
			# bottom right width elements
			self.space((width - 1) * 2 - 1)
			print("*")
			print("\n")
			i += 1
		end
		i = 1
		while (i <= width / 2)
		
			# bottom left width element
			self.space(i * 4)
			print("*")
			if (i != width / 2)
			
				# middle elements
				self.space((width - 1) * 2 - i * 4 - 1)
				print("*")
				# bottom right width elements
				self.space((width - 1) * 2 - i * 4 - 1)
				print("*")
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.print_cube(5)
	obj.print_cube(7)
	obj.print_cube(11)
	obj.print_cube(9)
end
main()

Output


Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
/*
  Scala Program
  Display cube star pattern
*/
class MyPattern
{
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" ");
			i += 1;
		}
	}
	def print_cube(width: Int): Unit = {
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		print("\n\nWidth : " + width + "\n\n");
		var i: Int = 0;
		while (i <= (width / 2).toInt)
		{
			space((width - 1) * 2 - (i) * 4);
			print("*");
			space(i * 8 - 1);
			if (i != 0)
			{
				print("*");
			}
			print("\n");
			i += 1;
		}
		i = 1;
		while (i <= (width / 2).toInt)
		{
			print("*");
			space(i * 4 - 1);
			print("*");
			space((width - 1) * 4 - 1 - i * 8);
			if (i < (width / 2).toInt)
			{
				print("*");
			}
			space(i * 4 - 1);
			print("*");
			print("\n");
			i += 1;
		}
		i = 1;
		while (i <= (width / 2).toInt)
		{
			print("*");
			//middle elements
			space((width - 1) * 2 - 1);
			print("*");
			//bottom right width elements
			space((width - 1) * 2 - 1);
			print("*");
			print("\n");
			i += 1;
		}
		i = 1;
		while (i <= (width / 2).toInt)
		{
			//bottom left width element
			space(i * 4);
			print("*");
			if (i != (width / 2).toInt)
			{
				//middle elements
				space((width - 1) * 2 - i * 4 - 1);
				print("*");
				//bottom right width elements
				space((width - 1) * 2 - i * 4 - 1);
				print("*");
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.print_cube(5);
		obj.print_cube(7);
		obj.print_cube(11);
		obj.print_cube(9);
	}
}

Output

Width : 5

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


Width : 7

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


Width : 11

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


Width : 9

                *
            *       *
        *               *
    *                       *
*                               *
*   *                       *   *
*       *               *       *
*           *       *           *
*               *               *
*               *               *
*               *               *
*               *               *
*               *               *
    *           *           *
        *       *       *
            *   *   *
                *
/*
  Swift Program
  Display cube star pattern
*/
class MyPattern
{
	//include space 
	func space(_ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	func print_cube(_ width: Int)
	{
		if (width % 2 == 0 || width <= 2)
		{
			return;
		}
		print("\n\nWidth : ", width ,"\n\n", terminator: "");
		var i: Int = 0;
		while (i <= width / 2)
		{
			self.space((width - 1) * 2 - (i) * 4);
			print("*", terminator: "");
			self.space(i * 8 - 1);
			if (i != 0)
			{
				print("*", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
		i = 1;
		while (i <= width / 2)
		{
			print("*", terminator: "");
			self.space(i * 4 - 1);
			print("*", terminator: "");
			self.space((width - 1) * 4 - 1 - i * 8);
			if (i < width / 2)
			{
				print("*", terminator: "");
			}
			self.space(i * 4 - 1);
			print("*", terminator: "");
			print("\n", terminator: "");
			i += 1;
		}
		i = 1;
		while (i <= width / 2)
		{
			print("*", terminator: "");
			//middle elements
			self.space((width - 1) * 2 - 1);
			print("*", terminator: "");
			//bottom right width elements
			self.space((width - 1) * 2 - 1);
			print("*", terminator: "");
			print("\n", terminator: "");
			i += 1;
		}
		i = 1;
		while (i <= width / 2)
		{
			//bottom left width element
			self.space(i * 4);
			print("*", terminator: "");
			if (i != width / 2)
			{
				//middle elements
				self.space((width - 1) * 2 - i * 4 - 1);
				print("*", terminator: "");
				//bottom right width elements
				self.space((width - 1) * 2 - i * 4 - 1);
				print("*", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.print_cube(5);
	obj.print_cube(7);
	obj.print_cube(11);
	obj.print_cube(9);
}
main();

Output

Width :  5

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


Width :  7

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


Width :  11

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


Width :  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