Print window pattern

Here given code implementation process.

//  C program for
//  Print window pattern
#include <stdio.h>

void printPattern(int height)
{
	printf("\n Height : %d  \n\n", height);
	// When height is Even
	int mid = height / 2;
	for (int i = 0; i < height; ++i)
	{
		for (int j = 0; j < height; ++j)
		{
			if (i == 0 || i + 1 == height || 
                j == 0 || j + 1 == height || 
                (i == mid) || (j == mid) || 
                (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
			{
				// Include star
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5);
	printPattern(9);
	printPattern(7);
	printPattern(8);
	return 0;
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
/*
    Java Program
    Print window pattern
*/
public class Pattern
{
	public void printPattern(int height)
	{
		System.out.println("\n Height : " + height + " \n");
		// When height is Even
		int mid = height / 2;
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j < height; ++j)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					System.out.print("* ");
				}
				else
				{
					System.out.print("  ");
				}
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print window pattern
*/
class Pattern
{
	public: void printPattern(int height)
	{
		cout << "\n Height : " << height << " \n" << endl;
		// When height is Even
		int mid = height / 2;
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j < height; ++j)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					cout << "* ";
				}
				else
				{
					cout << "  ";
				}
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(9);
	task->printPattern(7);
	task->printPattern(8);
	return 0;
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
// Include namespace system
using System;
/*
    Csharp Program
    Print window pattern
*/
public class Pattern
{
	public void printPattern(int height)
	{
		Console.WriteLine("\n Height : " + height + " \n");
		// When height is Even
		int mid = height / 2;
		for (int i = 0; i < height; ++i)
		{
			for (int j = 0; j < height; ++j)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					Console.Write("* ");
				}
				else
				{
					Console.Write("  ");
				}
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
package main
import "fmt"
/*
    Go Program
    Print window pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
func(this Pattern) printPattern(height int) {
	fmt.Println("\n Height : ", height, " \n")
	// When height is Even
	var mid int = height / 2
	for i := 0 ; i < height ; i++ {
		for j := 0 ; j < height ; j++ {
			if i == 0 || i + 1 == height || 
			j == 0 || j + 1 == height || 
			(i == mid) || (j == mid) || 
			(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)) {
				// Include star
				fmt.Print("* ")
			} else {
				fmt.Print("  ")
			}
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
<?php
/*
    Php Program
    Print window pattern
*/
class Pattern
{
	public	function printPattern($height)
	{
		echo("\n Height : ".$height." \n\n");
		// When height is Even
		$mid = (int)($height / 2);
		for ($i = 0; $i < $height; ++$i)
		{
			for ($j = 0; $j < $height; ++$j)
			{
				if ($i == 0 || $i + 1 == $height || 
                    $j == 0 || $j + 1 == $height || 
                    ($i == $mid) || ($j == $mid) || 
                    ($height % 2 == 0 && ($i + 1 == $mid || $j + 1 == $mid)))
				{
					// Include star
					echo("* ");
				}
				else
				{
					echo("  ");
				}
			}
			echo("\n");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(9);
	$task->printPattern(7);
	$task->printPattern(8);
}
main();

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
/*
    Node JS Program
    Print window pattern
*/
class Pattern
{
	printPattern(height)
	{
		console.log("\n Height : " + height + " \n");
		// When height is Even
		var mid = parseInt(height / 2);
		for (var i = 0; i < height; ++i)
		{
			for (var j = 0; j < height; ++j)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					process.stdout.write("* ");
				}
				else
				{
					process.stdout.write("  ");
				}
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}
main();

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
#    Python 3 Program
#    Print window pattern
class Pattern :
	def printPattern(self, height) :
		print("\n Height : ", height ," \n")
		#  When height is Even
		mid = int(height / 2)
		i = 0
		while (i < height) :
			j = 0
			while (j < height) :
				if (i == 0 or i + 1 == height or 
                    j == 0 or j + 1 == height or
                    (i == mid) or(j == mid) or
                (height % 2 == 0 and(i + 1 == mid or j + 1 == mid))) :
					#  Include star
					print("* ", end = "")
				else :
					print("  ", end = "")
				
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)

if __name__ == "__main__": main()

Output

 Height :  5

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

 Height :  9

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

 Height :  7

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

 Height :  8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
#    Ruby Program
#    Print window pattern
class Pattern 
	def printPattern(height) 
		print("\n Height : ", height ," \n", "\n")
		#  When height is Even
		mid = height / 2
		i = 0
		while (i < height) 
			j = 0
			while (j < height) 
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid))) 
					#  Include star
					print("* ")
				else
 
					print("  ")
				end

				j += 1
			end

			print("\n")
			i += 1
		end

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(7)
	task.printPattern(8)
end

main()

Output

 Height : 5 

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

 Height : 9 

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

 Height : 7 

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

 Height : 8 

* * * * * * * * 
*     * *     * 
*     * *     * 
* * * * * * * * 
* * * * * * * * 
*     * *     * 
*     * *     * 
* * * * * * * * 
/*
    Scala Program
    Print window pattern
*/
class Pattern()
{
	def printPattern(height: Int): Unit = {
		println("\n Height : " + height + " \n");
		// When height is Even
		var mid: Int = height / 2;
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j < height)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					print("* ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(7);
		task.printPattern(8);
	}
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
/*
    Swift 4 Program
    Print window pattern
*/
class Pattern
{
	func printPattern(_ height: Int)
	{
		print("\n Height : ", height ," \n");
		// When height is Even
		let mid: Int = height / 2;
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j < height)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					print("* ", terminator: "");
				}
				else
				{
					print("  ", terminator: "");
				}
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}
main();

Output

 Height :  5

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

 Height :  9

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

 Height :  7

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

 Height :  8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
/*
    Kotlin Program
    Print window pattern
*/
class Pattern
{
	fun printPattern(height: Int): Unit
	{
		println("\n Height : " + height + " \n");
		// When height is Even
		val mid: Int = height / 2;
		var i: Int = 0;
		while (i < height)
		{
			var j: Int = 0;
			while (j < height)
			{
				if (i == 0 || i + 1 == height || 
                    j == 0 || j + 1 == height || 
                    (i == mid) || (j == mid) || 
                    (height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
				{
					// Include star
					print("* ");
				}
				else
				{
					print("  ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(7);
	task.printPattern(8);
}

Output

 Height : 5

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

 Height : 9

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

 Height : 7

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

 Height : 8

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


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