Print N pattern

Here given code implementation process.

// C Program
// Display N pattern 
#include <stdio.h>

//include space 
void space(int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf(" ");
	}
}
void print_n(int size)
{
	if (size <= 3)
	{
		return;
	}
	printf("Size : %d\n\n", size);
	//Display Z pattern of given size
	for (int i = 0; i < size; i++)
	{
		printf("*");
		if (i != 0 && i != size - 1)
		{
			space(i - 1);
			printf("*");
			space((size - 2) - i);
		}
		else
		{
			space(size - 2);
		}
		printf("*");
		printf("\n");
	}
	printf("\n");
}
int main()
{
	//Test Case
	print_n(5);
	print_n(7);
	print_n(4);
	print_n(9);
	return 0;
}

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
// C++ program
// Display N 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_n(int size)
	{
		if (size <= 3)
		{
			return;
		}
		cout << "Size : " << size << "\n\n";
		//Display Z pattern of given size
		for (int i = 0; i < size; i++)
		{
			cout << "*";
			if (i != 0 && i != size - 1)
			{
				this->space(i - 1);
				cout << "*";
				this->space((size - 2) - i);
			}
			else
			{
				this->space(size - 2);
			}
			cout << "*";
			cout << "\n";
		}
		cout << "\n";
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Test Case
	obj.print_n(5);
	obj.print_n(7);
	obj.print_n(4);
	obj.print_n(9);
	return 0;
}

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
// C# program
// Display N pattern 
using System;
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write(" ");
		}
	}
	public void print_n(int size)
	{
		if (size <= 3)
		{
			return;
		}
		Console.Write("Size : " + size + "\n\n");
		//Display Z pattern of given size
		for (int i = 0; i < size; i++)
		{
			Console.Write("*");
			if (i != 0 && i != size - 1)
			{
				space(i - 1);
				Console.Write("*");
				space((size - 2) - i);
			}
			else
			{
				space(size - 2);
			}
			Console.Write("*");
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Case
		obj.print_n(5);
		obj.print_n(7);
		obj.print_n(4);
		obj.print_n(9);
	}
}

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
// Java program
// Display N pattern 
class MyPattern
{
	//include space 
	public void space(int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" ");
		}
	}
	public void print_n(int size)
	{
		if (size <= 3)
		{
			return;
		}
		System.out.print("Size : " + size + "\n\n");
		//Display Z pattern of given size
		for (int i = 0; i < size; i++)
		{
			System.out.print("*");
			if (i != 0 && i != size - 1)
			{
				space(i - 1);
				System.out.print("*");
				space((size - 2) - i);
			}
			else
			{
				space(size - 2);
			}
			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_n(5);
		obj.print_n(7);
		obj.print_n(4);
		obj.print_n(9);
	}
}

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
<?php
// Php program
// Display N pattern 
class MyPattern
{
	//include space 
	public 	function space($size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo(" ");
		}
	}
	public 	function print_n($size)
	{
		if ($size <= 3)
		{
			return;
		}
		echo("Size : ". $size ."\n\n");
		//Display Z pattern of given size
		for ($i = 0; $i < $size; $i++)
		{
			echo("*");
			if ($i != 0 && $i != $size - 1)
			{
				$this->space($i - 1);
				echo("*");
				$this->space(($size - 2) - $i);
			}
			else
			{
				$this->space($size - 2);
			}
			echo("*");
			echo("\n");
		}
		echo("\n");
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Case
	$obj->print_n(5);
	$obj->print_n(7);
	$obj->print_n(4);
	$obj->print_n(9);
}
main();

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
// Node Js program
// Display N pattern 
class MyPattern
{
	//include space 
	space(size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" ");
		}
	}
	print_n(size)
	{
		if (size <= 3)
		{
			return;
		}
		process.stdout.write("Size : " + size + "\n\n");
		//Display Z pattern of given size
		for (var i = 0; i < size; i++)
		{
			process.stdout.write("*");
			if (i != 0 && i != size - 1)
			{
				this.space(i - 1);
				process.stdout.write("*");
				this.space((size - 2) - i);
			}
			else
			{
				this.space(size - 2);
			}
			process.stdout.write("*");
			process.stdout.write("\n");
		}
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Case
	obj.print_n(5);
	obj.print_n(7);
	obj.print_n(4);
	obj.print_n(9);
}
main();

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
#  Python 3 program
#  Display N pattern 
class MyPattern :
	# include space 
	def space(self, size) :
		i = 0
		while (i < size) :
			print(" ", end = "")
			i += 1
		
	
	def print_n(self, size) :
		if (size <= 3) :
			return
		
		print("Size : ", size ,"\n\n", end = "")
		# Display Z pattern of given size
		i = 0
		while (i < size) :
			print("*", end = "")
			if (i != 0 and i != size - 1) :
				self.space(i - 1)
				print("*", end = "")
				self.space((size - 2) - i)
			else :
				self.space(size - 2)
			
			print("*", end = "")
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MyPattern()
	# Test Case
	obj.print_n(5)
	obj.print_n(7)
	obj.print_n(4)
	obj.print_n(9)


if __name__ == "__main__": main()

Output

Size :  5

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

Size :  7

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

Size :  4

*  *
** *
* **
*  *

Size :  9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
#  Ruby program
#  Display N pattern 
class MyPattern

	# include space 
	def space(size)
	
		i = 0
		while (i < size)
		
			print(" ")
			i += 1
		end
	end
	def print_n(size)
	
		if (size <= 3)
		
			return
		end
		print("Size  : ", size ,"\n\n")
		# Display Z pattern of given size
		i = 0
		while (i < size)
		
			print("*")
			if (i != 0 && i != size - 1)
			
				self.space(i - 1)
				print("*")
				self.space((size - 2) - i)
			else
			
				self.space(size - 2)
			end
			print("*")
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main()

	obj = MyPattern.new()
	# Test Case
	obj.print_n(5)
	obj.print_n(7)
	obj.print_n(4)
	obj.print_n(9)
end
main()

Output

Size  : 5

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

Size  : 7

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

Size  : 4

*  *
** *
* **
*  *

Size  : 9

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

// Scala program
// Display N pattern 
class MyPattern
{
	//include space 
	def space(size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" ");
			i += 1;
		}
	}
	def print_n(size: Int): Unit = {
		if (size <= 3)
		{
			return;
		}
		print("Size : " + size + "\n\n");
		//Display Z pattern of given size
		var i: Int = 0;
		while (i < size)
		{
			print("*");
			if (i != 0 && i != size - 1)
			{
				space(i - 1);
				print("*");
				space((size - 2) - i);
			}
			else
			{
				space(size - 2);
			}
			print("*");
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Case
		obj.print_n(5);
		obj.print_n(7);
		obj.print_n(4);
		obj.print_n(9);
	}
}

Output

Size : 5

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

Size : 7

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

Size : 4

*  *
** *
* **
*  *

Size : 9

*       *
**      *
* *     *
*  *    *
*   *   *
*    *  *
*     * *
*      **
*       *
// Swift program
// Display N pattern 
class MyPattern
{
	//include space 
	func space(_ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	func print_n(_ size: Int)
	{
		if (size <= 3)
		{
			return;
		}
		print("Size : ", size ,"\n\n", terminator: "");
		//Display Z pattern of given size
		var i: Int = 0;
		while (i < size)
		{
			print("*", terminator: "");
			if (i != 0 && i != size - 1)
			{
				self.space(i - 1);
				print("*", terminator: "");
				self.space((size - 2) - i);
			}
			else
			{
				self.space(size - 2);
			}
			print("*", terminator: "");
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Case
	obj.print_n(5);
	obj.print_n(7);
	obj.print_n(4);
	obj.print_n(9);
}
main();

Output

Size :  5

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

Size :  7

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

Size :  4

*  *
** *
* **
*  *

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