Skip to main content

Display S symbol pattern

Here given code implementation process.

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

//Display 'S' pattern of given Even size height
void show_s(int height)
{
	if (height <= 4 || height % 2 != 0)
	{
		// When number is less than 4 or height is not Even size
		return;
	}
	printf("Height : %d \n\n", height);
	for (int i = 1; i < height; ++i)
	{
		for (int j = 1; j < height; ++j)
		{
			// Condition which is print the star pattern
			if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
			{
				printf("* ");
				j += 1;
			}
			else
			{
				//Include space
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("\n");
}
int main()
{
	//Test Cases
	show_s(10);
	show_s(16);
	show_s(6);
	return 0;
}

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
// Java Program
// Display S pattern
class MyPattern
{
	//Display 'S' pattern of given Even size height
	public void show_s(int height)
	{
		if (height <= 4 || height % 2 != 0)
		{
			// When number is less than 4 or height is not Even size
			return;
		}
		System.out.print("Height : " + height + " \n\n");
		for (int i = 1; i < height; ++i)
		{
			for (int j = 1; j < height; ++j)
			{
				// Condition which is print the star pattern
				if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
				{
					System.out.print("* ");
					j += 1;
				}
				else
				{
					//Include space
					System.out.print(" ");
				}
			}
			System.out.print("\n");
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.show_s(10);
		obj.show_s(16);
		obj.show_s(6);
	}
}

Output

Height : 10

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

Height : 16

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

Height : 6

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

using namespace std;
class MyPattern
{
	public:
		//Display 'S' pattern of given Even size height
		void show_s(int height)
		{
			if (height <= 4 || height % 2 != 0)
			{
				// When number is less than 4 or height is not Even size
				return;
			}
			cout << "Height : " << height << " \n\n";
			for (int i = 1; i < height; ++i)
			{
				for (int j = 1; j < height; ++j)
				{
					// Condition which is print the star pattern
					if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
					{
						cout << "* ";
						j += 1;
					}
					else
					{
						cout << " ";
					}
				}
				cout << "\n";
			}
			cout << "\n";
		}
};
int main()
{
	MyPattern obj ;
	//Test Cases
	obj.show_s(10);
	obj.show_s(16);
	obj.show_s(6);
	return 0;
}

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
// C# Program
// Display S pattern
using System;
class MyPattern
{
	//Display 'S' pattern of given Even size height
	public void show_s(int height)
	{
		if (height <= 4 || height % 2 != 0)
		{
			// When number is less than 4 or height is not Even size
			return;
		}
		Console.Write("Height : " + height + " \n\n");
		for (int i = 1; i < height; i++)
		{
			for (int j = 1; j < height; j++)
			{
				// Condition which is print the star pattern
				if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
				{
					Console.Write("* ");
					j += 1;
				}
				else
				{
					Console.Write(" ");
				}
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.show_s(10);
		obj.show_s(16);
		obj.show_s(6);
	}
}

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
<?php
// Php Program
// Display S pattern
class MyPattern
{
	//Display 'S' pattern of given Even size height
	public 	function show_s($height)
	{
		if ($height <= 4 || $height % 2 != 0)
		{
			return;
		}
		echo("Height : ". $height ." \n\n");
		for ($i = 1; $i < $height; ++$i)
		{
			for ($j = 1; $j < $height; ++$j)
			{
				// Condition which is print the star pattern
				if ($i + 1 == $height || intval($height / 2) == $i || ($i < intval($height / 2) && $j == 1) || ($i > intval($height / 2) && $j + 1 == $height) || $i == 1)
				{
					echo("* ");
					$j += 1;
				}
				else
				{
					//Include space
					echo(" ");
				}
			}
			echo("\n");
		}
		echo("\n");
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->show_s(10);
	$obj->show_s(16);
	$obj->show_s(6);
}
main();

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
// Node Js Program
// Display S pattern
class MyPattern
{
	//Display 'S' pattern of given Even size height
	show_s(height)
	{
		if (height <= 4 || height % 2 != 0)
		{
			return;
		}
		process.stdout.write("Height : " + height + " \n\n");
		for (var i = 1; i < height; ++i)
		{
			for (var j = 1; j < height; ++j)
			{
				// Condition which is print the star pattern
				if (i + 1 == height || parseInt(height / 2) == i || (i < parseInt(height / 2) && j == 1) || (i > parseInt(height / 2) && j + 1 == height) || i == 1)
				{
					process.stdout.write("* ");
					j += 1;
				}
				else
				{
					//Include space
					process.stdout.write(" ");
				}
			}
			process.stdout.write("\n");
		}
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.show_s(10);
	obj.show_s(16);
	obj.show_s(6);
}
main();

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
#  Python 3 Program
#  Display S pattern
class MyPattern :
	# Display 'S' pattern of given Even size height
	def show_s(self, height) :
		if (height <= 4 or height % 2 != 0) :
			return
		
		print("Height : ", height ," \n\n", end = "")
		i = 1
		j = 0
		while (i < height) :
			j = 1
			while (j < height) :
				#  Condition which is print the star pattern
				if (i + 1 == height or int(height / 2) == i or(i < int(height / 2) and j == 1) or(i > int(height / 2) and j + 1 == height) or i == 1) :
					print("* ", end = "")
					j += 1
				else :
					print(" ", end = "")
				
				j += 1
			
			print(end = "\n")
			i += 1
		
		print(end = "\n")
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.show_s(10)
	obj.show_s(16)
	obj.show_s(6)


if __name__ == "__main__": main()

Output

Height :  10

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

Height :  16

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

Height :  6

* * *
*
* * *
    *
* * *
#  Ruby Program
#  Display S pattern
class MyPattern

	# Display 'S' pattern of given Even size height
	def show_s(height)
	
		if (height <= 4 || height % 2 != 0)
		
			return
		end
		print("Height  :", height ," \n\n")
		i = 1
		j = 0
		while (i < height)
		
			j = 1
			while (j < height)
			
				#  Condition which is print the star pattern
				if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
				
					print("* ")
					j += 1
				else
				
					print(" ")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.show_s(10)
	obj.show_s(16)
	obj.show_s(6)
end
main()

Output

Height  :10 

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

Height  :16 

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

Height  :6 

* * * 
*    
* * * 
    * 
* * * 

// Scala Program
// Display S pattern
class MyPattern
{
	//Display 'S' pattern of given Even size height
	def show_s(height: Int): Unit = {
		if (height <= 4 || height % 2 != 0)
		{
			return;
		}
		print("Height : " + height + " \n\n");
		var i: Int = 1;
		var j: Int = 0;
		while (i < height)
		{
			j = 1;
			while (j < height)
			{
				// Condition which is print the star pattern
				if (i + 1 == height || (height / 2).toInt == i || (i < (height / 2).toInt && j == 1) || (i > (height / 2).toInt && j + 1 == height) || i == 1)
				{
					print("* ");
					j += 1;
				}
				else
				{
					print(" ");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.show_s(10);
		obj.show_s(16);
		obj.show_s(6);
	}
}

Output

Height : 10

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

Height : 16

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

Height : 6

* * *
*
* * *
    *
* * *
// Swift Program
// Display S pattern
class MyPattern
{
	//Display "S" pattern of given Even size height
	func show_s(_ height: Int)
	{
		if (height <= 4 || height % 2 != 0)
		{
			return;
		}
		print("Height : ", height ," \n");
		var i: Int = 1;
		var j: Int = 0;
		while (i < height)
		{
			j = 1;
			while (j < height)
			{
				// Condition which is print the star pattern
				if (i + 1 == height || height / 2 == i || (i < height / 2 && j == 1) || (i > height / 2 && j + 1 == height) || i == 1)
				{
					print("*", terminator: " ");
					j += 1;
				}
				else
				{
					print(terminator: " ");
				}
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.show_s(10);
	obj.show_s(16);
	obj.show_s(6);
}
main();

Output

Height :  10

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

Height :  16

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

Height :  6

* * *
*
* * *
    *
* * *




Comment

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