Display S symbol pattern
The "S" symbol pattern is a pattern that resembles the letter "S" and is formed using asterisks (*) and spaces. The program provided displays the "S" pattern for a given even height. The height determines the size of the pattern. The code is implemented in the C programming language.
Problem Statement
The goal is to create a program that generates an "S" pattern with a given even height. The pattern consists of asterisks (*) arranged in a specific manner to form the shape of the letter "S". The program should only accept even heights greater than 4 as input. If an invalid height is provided, the program should not generate any output.
For example, if the input height is 10, the program should produce the following output:
* * * * * * * * * * * * * * * * * * * * *
Algorithm
The algorithm to generate the "S" pattern can be described as follows:
- Check if the given height is less than or equal to 4 or if it is an odd number. If either condition is true, return without generating any output.
- Print the height value for reference.
- Iterate over each row from 1 to height - 1:
- Iterate over each column from 1 to height - 1:
- If the current position satisfies any of the conditions to print an asterisk (*), print "*" and increment the column by 1.
- Otherwise, print a space character to maintain the pattern shape.
- Print a new line after each row iteration.
Pseudocode
show_s(height):
if height <= 4 or height % 2 != 0:
return
print "Height: " + height
for i in range(1, height):
for j in range(1, height):
if i + 1 == height or height / 2 == i or (i < height / 2 and j == 1) or (i > height / 2 and j + 1 == height) or i == 1:
print "* ",
j += 1
else:
print " ",
print new line
main():
show_s(10)
show_s(16)
show_s(6)
Code Solution
//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
* * *
*
* * *
*
* * *
Time Complexity
The time complexity of the algorithm is O(height^2) since it involves nested loops iterating from 1 to the given height.
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