Print the R pattern

Here given code implementation process.

//C Program 
//Print R pattern
#include <stdio.h>

//Include star of given size
void print_star(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		if (counter % 2 == 0)
		{
			//Add Star
			printf("*");
		}
		else
		{
			//Add Space
			printf(" ");
		}
	}
}
//Display space of given size
void print_space(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		//Add space
		printf(" ");
	}
}
//Print 'R' star pattern of given size
void show_r(int height)
{
	if (height < 3 || height % 2 == 0)
	{
		return;
	}
	printf("\nHeight : %d  \n\n", height);
	for (int i = 0; i < height; ++i)
	{
		if (i == 0 || i == height / 2)
		{
			//when printed the star in horizontal direction
			print_star(height);
			print_space(3);
		}
		else
		{
			//print boundary elements
			printf("*");
			print_space(height - 1);
			printf("*");
			print_space(2);
		}
		printf("\n");
	}
}
int main()
{
	//Simple test
	show_r(7);
	show_r(9);
	show_r(3);
	show_r(15);
	return 0;
}

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
/*
  Java Program
  Print R star pattern
*/
class MyPattern
{
	//Include star of given size
	public void print_star(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			if (counter % 2 == 0)
			{
				//Add Star
				System.out.print("*");
			}
			else
			{
				//Add Space
				System.out.print(" ");
			}
		}
	}
	//Display space of given size
	public void print_space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			System.out.print(" ");
		}
	}
	//Print 'R' star pattern of given size
	public void show_r(int height)
	{
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		System.out.print("\nHeight : " + height + " \n\n");
		for (int i = 0; i < height; ++i)
		{
			if (i == 0 || i == height / 2)
			{
				//when printed the star in horizontal direction
				print_star(height);
				print_space(3);
			}
			else
			{
				//print boundary elements
				System.out.print("*");
				print_space(height - 1);
				System.out.print("*");
				print_space(2);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.show_r(7);
		obj.show_r(9);
		obj.show_r(3);
		obj.show_r(15);
	}
}

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
/*
  C++ Program
  Print R star pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public:
		//Include star of given size
		void print_star(int size)
		{
			int counter = 0;
			for (counter = 0; counter < size; counter++)
			{
				if (counter % 2 == 0)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
			}
		}
	//Display space of given size
	void print_space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << " ";
		}
	}
	//Print 'R' star pattern of given size
	void show_r(int height)
	{
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		cout << "\nHeight : " << height << " \n\n";
		for (int i = 0; i < height; ++i)
		{
			if (i == 0 || i == height / 2)
			{
				//when printed the star in horizontal direction
				this->print_star(height);
				this->print_space(3);
			}
			else
			{
				cout << "*";
				this->print_space(height - 1);
				cout << "*";
				this->print_space(2);
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Simple test
	obj.show_r(7);
	obj.show_r(9);
	obj.show_r(3);
	obj.show_r(15);
	return 0;
}

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
/*
  C# Program
  Print R star pattern
*/
using System;
class MyPattern
{
	//Include star of given size
	public void print_star(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			if (counter % 2 == 0)
			{
				//Add Star
				Console.Write("*");
			}
			else
			{
				//Add Space
				Console.Write(" ");
			}
		}
	}
	//Display space of given size
	public void print_space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			Console.Write(" ");
		}
	}
	//Print 'R' star pattern of given size
	public void show_r(int height)
	{
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		Console.Write("\nHeight : " + height + " \n\n");
		for (int i = 0; i < height; i++)
		{
			if (i == 0 || i == height / 2)
			{
				//when printed the star in horizontal direction
				print_star(height);
				print_space(3);
			}
			else
			{
				//print boundary elements
				Console.Write("*");
				print_space(height - 1);
				Console.Write("*");
				print_space(2);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.show_r(7);
		obj.show_r(9);
		obj.show_r(3);
		obj.show_r(15);
	}
}

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
<?php
/*
  Php Program
  Print R star pattern
*/
class MyPattern
{
	//Include star of given size
	public	function print_star($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			if ($counter % 2 == 0)
			{
				//Add Star
				echo("*");
			}
			else
			{
				//Add Space
				echo(" ");
			}
		}
	}
	//Display space of given size
	public	function print_space($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			//Add space
			echo(" ");
		}
	}
	//Print 'R' star pattern of given size
	public	function show_r($height)
	{
		if ($height < 3 || $height % 2 == 0)
		{
			return;
		}
		echo("\nHeight : ". $height ." \n\n");
		for ($i = 0; $i < $height; ++$i)
		{
			if ($i == 0 || $i == intval($height / 2))
			{
				//when printed the star in horizontal direction
				$this->print_star($height);
				$this->print_space(3);
			}
			else
			{
				//print boundary elements
				echo("*");
				$this->print_space($height - 1);
				echo("*");
				$this->print_space(2);
			}
			echo("\n");
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->show_r(7);
	$obj->show_r(9);
	$obj->show_r(3);
	$obj->show_r(15);
}
main();

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
/*
  Node Js Program
  Print R star pattern
*/
class MyPattern
{
	//Include star of given size
	print_star(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			if (counter % 2 == 0)
			{
				//Add Star
				process.stdout.write("*");
			}
			else
			{
				//Add Space
				process.stdout.write(" ");
			}
		}
	}
	//Display space of given size
	print_space(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			process.stdout.write(" ");
		}
	}
	//Print 'R' star pattern of given size
	show_r(height)
	{
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		process.stdout.write("\nHeight : " + height + " \n\n");
		for (var i = 0; i < height; ++i)
		{
			if (i == 0 || i == parseInt(height / 2))
			{
				//when printed the star in horizontal direction
				this.print_star(height);
				this.print_space(3);
			}
			else
			{
				//print boundary elements
				process.stdout.write("*");
				this.print_space(height - 1);
				process.stdout.write("*");
				this.print_space(2);
			}
			process.stdout.write("\n");
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Simple test
	obj.show_r(7);
	obj.show_r(9);
	obj.show_r(3);
	obj.show_r(15);
}
main();

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
# Python 3 Program
# Print R star pattern

class MyPattern :
	# Include star of given size
	def print_star(self, size) :
		counter = 0
		while (counter < size) :
			if (counter % 2 == 0) :
				print("*", end = "")
			else :
				print(" ", end = "")
			
			counter += 1
		
	
	# Display space of given size
	def print_space(self, size) :
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	# Print 'R' star pattern of given size
	def show_r(self, height) :
		if (height < 3 or height % 2 == 0) :
			return
		
		print("\nHeight : ", height ," \n\n", end = "")
		i = 0
		while (i < height) :
			if (i == 0 or i == int(height / 2)) :
				# when printed the star in horizontal direction
				self.print_star(height)
				self.print_space(3)
			else :
				print("*", end = "")
				self.print_space(height - 1)
				print("*", end = "")
				self.print_space(2)
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.show_r(7)
	obj.show_r(9)
	obj.show_r(3)
	obj.show_r(15)


if __name__ == "__main__": main()

Output

Height :  7

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

Height :  9

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

Height :  3

* *
* *
*  *

Height :  15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
# Ruby Program
# Print R star pattern

class MyPattern

	# Include star of given size
	def print_star(size)
	
		counter = 0
		while (counter < size)
		
			if (counter % 2 == 0)
			
				print("*")
			else
			
				print(" ")
			end
			counter += 1
		end
	end
	# Display space of given size
	def print_space(size)
	
		counter = 0
		while (counter < size)
		
			print(" ")
			counter += 1
		end
	end
	# Print 'R' star pattern of given size
	def show_r(height)
	
		if (height < 3 || height % 2 == 0)
		
			return
		end
		print("\nHeight : ", height ," \n\n")
		i = 0
		while (i < height)
		
			if (i == 0 || i == height / 2)
			
				# when printed the star in horizontal direction
				self.print_star(height)
				self.print_space(3)
			else
			
				print("*")
				self.print_space(height - 1)
				print("*")
				self.print_space(2)
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.show_r(7)
	obj.show_r(9)
	obj.show_r(3)
	obj.show_r(15)
end
main()

Output

Height : 7 

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

Height : 9 

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

Height : 3 

* *   
* *   
*  *  

Height : 15 

* * * * * * * *   
*              *  
*              *  
*              *  
*              *  
*              *  
*              *  
* * * * * * * *   
*              *  
*              *  
*              *  
*              *  
*              *  
*              *  
*              *  
/*
  Scala Program
  Print R star pattern
*/
class MyPattern
{
	//Include star of given size
	def print_star(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			if (counter % 2 == 0)
			{
				print("*");
			}
			else
			{
				print(" ");
			}
			counter += 1;
		}
	}
	//Display space of given size
	def print_space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ");
			counter += 1;
		}
	}
	//Print 'R' star pattern of given size
	def show_r(height: Int): Unit = {
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		print("\nHeight : " + height + " \n\n");
		var i: Int = 0;
		while (i < height)
		{
			if (i == 0 || i == (height / 2).toInt)
			{
				//when printed the star in horizontal direction
				print_star(height);
				print_space(3);
			}
			else
			{
				print("*");
				print_space(height - 1);
				print("*");
				print_space(2);
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.show_r(7);
		obj.show_r(9);
		obj.show_r(3);
		obj.show_r(15);
	}
}

Output

Height : 7

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

Height : 9

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

Height : 3

* *
* *
*  *

Height : 15

* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
* * * * * * * *
*              *
*              *
*              *
*              *
*              *
*              *
*              *
/*
  Swift Program
  Print R star pattern
*/
class MyPattern
{
	//Include star of given size
	func print_star(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			if (counter % 2 == 0)
			{
				print("*", terminator: "");
			}
			else
			{
				print(" ", terminator: "");
			}
			counter += 1;
		}
	}
	//Display space of given size
	func print_space(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ", terminator: "");
			counter += 1;
		}
	}
	//Print "R" star pattern of given size
	func show_r(_ height: Int)
	{
		if (height < 3 || height % 2 == 0)
		{
			return;
		}
		print("\nHeight : ", height ," \n\n", terminator: "");
		var i: Int = 0;
		while (i < height)
		{
			if (i == 0 || i == height / 2)
			{
				//when printed the star in horizontal direction
				self.print_star(height);
				self.print_space(3);
			}
			else
			{
				print("*", terminator: "");
				self.print_space(height - 1);
				print("*", terminator: "");
				self.print_space(2);
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.show_r(7);
	obj.show_r(9);
	obj.show_r(3);
	obj.show_r(15);
}
main();

Output

Height :  7

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

Height :  9

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

Height :  3

* *
* *
*  *

Height :  15

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


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