Print collate shape of given size

Here given code implementation process.

//C Program 
//Print collate shape of given size
#include <stdio.h>

//Print collate star pattern of given size
void print_collate(int size)
{
	if (size <= 2)
	{
		return;
	}
	printf("\nsize : %d  \n\n", size);
	//Loop controlling variables
	int i = 0, j = 0;
	//Loop, which is control the printing row operations
	for (i = 0; i < size; ++i)
	{
		//Loop, which is control the printing column operations
		for (j = 0; j < size; ++j)
		{
			//Condition which is printing a star element
			if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
			{
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
int main()
{
	/*
	Example size 9

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


	*/
	//Simple test
	print_collate(9);
	print_collate(7);
	print_collate(10);
	return 0;
}

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Java program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	public void print_collate(int size)
	{
		if (size <= 2)
		{
			return;
		}
		System.out.print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					System.out.print("* ");
				}
				else
				{
					System.out.print("  ");
				}
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
//Include header file
#include <iostream>

using namespace std;
/*
C++ program
Print collate shape of given size
*/
class MyPattern
{
	public:
		//Print collate star pattern of given size
		void print_collate(int size)
		{
			if (size <= 2)
			{
				return;
			}
			cout << "\nsize : " << size << " \n\n";
			//Loop controlling variables
			int i = 0, j = 0;
			//Loop, which is control the printing row operations
			for (i = 0; i < size; ++i)
			{
				//Loop, which is control the printing column operations
				for (j = 0; j < size; ++j)
				{
					//Condition which is printing a star element
					if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
					{
						cout << "* ";
					}
					else
					{
						cout << "  ";
					}
				}
				cout << "\n";
			}
		}
};
int main()
{
	MyPattern obj = MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
	return 0;
}

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
C# program
Print collate shape of given size
*/
//Include namespace system
using System;
class MyPattern
{
	//Print collate star pattern of given size
	public void print_collate(int size)
	{
		if (size <= 2)
		{
			return;
		}
		Console.Write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		int i = 0, j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					Console.Write("* ");
				}
				else
				{
					Console.Write("  ");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
<?php
/*
Php program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	public	function print_collate($size)
	{
		if ($size <= 2)
		{
			return;
		}
		echo "\nsize : ". $size ." \n\n";
		//Loop controlling variables
		$i = 0;
		$j = 0;
		//Loop, which is control the printing row operations
		for ($i = 0; $i < $size; ++$i)
		{
			//Loop, which is control the printing column operations
			for ($j = 0; $j < $size; ++$j)
			{
				//Condition which is printing a star element
				if ($i == 0 || $j == $i || $i == $size - 1 || $size - 1 - $i == $j)
				{
					echo "* ";
				}
				else
				{
					echo "  ";
				}
			}
			echo "\n";
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->print_collate(9);
	$obj->print_collate(7);
	$obj->print_collate(10);
}
main();

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Node Js program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	print_collate(size)
	{
		if (size <= 2)
		{
			return;
		}
		process.stdout.write("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i = 0;
		var j = 0;
		//Loop, which is control the printing row operations
		for (i = 0; i < size; ++i)
		{
			//Loop, which is control the printing column operations
			for (j = 0; j < size; ++j)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					process.stdout.write("* ");
				}
				else
				{
					process.stdout.write("  ");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var obj = new MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
}
main();

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
# Python 3 program
# Print collate shape of given size

class MyPattern :
	# Print collate star pattern of given size
	def print_collate(self, size) :
		if (size <= 2) :
			return
		
		print("\nsize : ", size ," \n\n", end = "")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size) :
			j = 0
			# Loop, which is control the printing column operations
			while (j < size) :
				# Condition which is printing a star element
				if (i == 0 or j == i or i == size - 1 or size - 1 - i == j) :
					print("* ", end = "")
				else :
					print("  ", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.print_collate(9)
	obj.print_collate(7)
	obj.print_collate(10)

if __name__ == "__main__": main()

Output

size :  9

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

size :  7

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

size :  10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
# Ruby program
# Print collate shape of given size

class MyPattern

	# Print collate star pattern of given size
	def print_collate(size)
	
		if (size <= 2)
		
			return
		end
		print("\nsize : ", size ," \n\n")
		# Loop controlling variables
		i = 0
		j = 0
		# Loop, which is control the printing row operations
		while (i < size)
		
			j = 0
			# Loop, which is control the printing column operations
			while (j < size)
			
				# Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				
					print("* ")
				else
				
					print("  ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.print_collate(9)
	obj.print_collate(7)
	obj.print_collate(10)
end
main()

Output

size : 9 

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

size : 7 

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

size : 10 

* * * * * * * * * * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
* * * * * * * * * * 
/*
Scala program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	def print_collate(size: Int): Unit = {
		if (size <= 2)
		{
			return;
		}
		print("\nsize : " + size + " \n\n");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					print("* ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.print_collate(9);
		obj.print_collate(7);
		obj.print_collate(10);
	}
}

Output

size : 9

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

size : 7

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

size : 10

* * * * * * * * * *
  *             *
    *         *
      *     *
        * *
        * *
      *     *
    *         *
  *             *
* * * * * * * * * *
/*
Swift program
Print collate shape of given size
*/
class MyPattern
{
	//Print collate star pattern of given size
	func print_collate(_ size: Int)
	{
		if (size <= 2)
		{
			return;
		}
		print("\nsize : ", size ," \n\n", terminator: "");
		//Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Loop, which is control the printing row operations
		while (i < size)
		{
			j = 0;
			//Loop, which is control the printing column operations
			while (j < size)
			{
				//Condition which is printing a star element
				if (i == 0 || j == i || i == size - 1 || size - 1 - i == j)
				{
					print("* ", terminator: "");
				}
				else
				{
					print("  ", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.print_collate(9);
	obj.print_collate(7);
	obj.print_collate(10);
}
main();

Output

size :  9

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

size :  7

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

size :  10

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


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