Print M pattern
In this article, we will discuss how to print the M pattern using a C program. The M pattern consists of asterisks (*) arranged in the shape of the letter M. We will explain the problem statement, provide a suitable example, discuss the algorithm and pseudocode, and analyze the time complexity of the code. Let's get started!
Problem Statement
The task is to write a program that takes an integer value as input and prints the M pattern using asterisks. The size of the pattern determines the height of the M.
Example
For the input size = 5, the output will be:
* * * * * * * * * * * * *
For the input size = 9, the output will be:
* * * * * * * * * * * * * * * * * * * * * * * * *
Algorithm and Pseudocode
To solve this problem, we can use a nested loop to iterate over each position in the pattern. The outer loop will iterate for each row, and the inner loop will iterate for each column.
1. Start by checking if the given size is valid. If the size is less than or equal to 2 or if it is an even number, return without printing anything.
2. Calculate the midpoint of the pattern by dividing the size by 2.
3. Iterate over each row using the variable 'i' from 0 to size-1.
4. For each row, iterate over each column using the variable 'j' from 0 to size-1.
5. Inside the inner loop, check the conditions for printing an asterisk (*):
- a. If the column is the first or the last column (j == 0 or j == size - 1), print an asterisk.
- b. If the row is within the top half of the pattern and is equal to the column (i <= mid && i == j), print an asterisk.
- c. If the row is within the top half of the pattern, is less than the midpoint, and the mirrored position of the row (size - 1 - i) is equal to the column (i < mid && size - 1 - i == j), print an asterisk.
6. If none of the conditions are met, print two spaces to maintain the shape of the M pattern.
7. After printing each row, move to the next line.
Here is the pseudocode for the algorithm:
function print_m(size):
if size <= 2 or size is even:
return
mid = size / 2
for i = 0 to size-1:
for j = 0 to size-1:
if j == 0 or j == size - 1 or (i <= mid and i == j) or (i < mid and size - 1 - i == j):
print " *"
else:
print " "
move to the next line
Code Solution
Here given code implementation process.
//C Program
//Print M pattern
#include <stdio.h>
void print_m(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
printf("\nHeight : %d\n\n", size);
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
printf(" *");
}
else
{
//include double space
printf(" ");
}
}
printf("\n");
}
}
int main()
{
//Simple test
print_m(5);
print_m(9);
print_m(13);
return 0;
}
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
Java Program
Print M pattern
*/
class MyPattern
{
public void print_m(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
System.out.print("\nHeight : " + size + "\n\n");
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
System.out.print(" *");
}
else
{
//include double space
System.out.print(" ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
}
}
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
C++ Program
Print M pattern
*/
#include<iostream>
using namespace std;
class MyPattern
{
public: void print_m(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
cout << "\nHeight : " << size << "\n\n";
int mid = size / 2;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
cout << " *";
}
else
{
cout << " ";
}
}
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
return 0;
}
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
C# Program
Print M pattern
*/
using System;
class MyPattern
{
public void print_m(int size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
Console.Write("\nHeight : " + size + "\n\n");
int mid = size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
Console.Write(" *");
}
else
{
//include double space
Console.Write(" ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
}
}
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
<?php
/*
Php Program
Print M pattern
*/
class MyPattern
{
public function print_m($size)
{
if ($size <= 2 || $size % 2 == 0)
{
return;
}
echo("\nHeight : ". $size ."\n\n");
$mid = intval($size / 2);
for ($i = 0; $i < $size; ++$i)
{
for ($j = 0; $j < $size; ++$j)
{
//Test case which is printing the value of star
if ($j == 0 || $j == $size - 1 || ($i <= $mid && $i == $j) || ($i < $mid && $size - 1 - $i == $j))
{
echo(" *");
}
else
{
//include double space
echo(" ");
}
}
echo("\n");
}
}
}
function main()
{
$obj = new MyPattern();
//Simple test
$obj->print_m(5);
$obj->print_m(9);
$obj->print_m(13);
}
main();
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
Node Js Program
Print M pattern
*/
class MyPattern
{
print_m(size)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
process.stdout.write("\nHeight : " + size + "\n\n");
var mid = parseInt(size / 2);
for (var i = 0; i < size; ++i)
{
for (var j = 0; j < size; ++j)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
process.stdout.write(" *");
}
else
{
//include double space
process.stdout.write(" ");
}
}
process.stdout.write("\n");
}
}
}
function main(args)
{
var obj = new MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
}
main();
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
# Python 3 Program
# Print M pattern
class MyPattern :
def print_m(self, size) :
if (size <= 2 or size % 2 == 0) :
return
print("\nHeight : ", size ,"\n\n", end = "")
mid = int(size / 2)
i = 0
j = 0
while (i < size) :
j = 0
while (j < size) :
# Test case which is printing the value of star
if (j == 0 or j == size - 1 or(i <= mid and i == j) or(i < mid and size - 1 - i == j)) :
print(" *", end = "")
else :
print(" ", end = " ")
j += 1
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
# Simple test
obj.print_m(5)
obj.print_m(9)
obj.print_m(13)
if __name__ == "__main__": main()
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
# Ruby Program
# Print M pattern
class MyPattern
def print_m(size)
if (size <= 2 || size % 2 == 0)
return
end
print("\nHeight : ", size ,"\n\n")
mid = size / 2
i = 0
j = 0
while (i < size)
j = 0
while (j < size)
# Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
print(" *")
else
print(" ")
end
j += 1
end
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
# Simple test
obj.print_m(5)
obj.print_m(9)
obj.print_m(13)
end
main()
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
Scala Program
Print M pattern
*/
class MyPattern
{
def print_m(size: Int): Unit = {
if (size <= 2 || size % 2 == 0)
{
return;
}
print("\nHeight : " + size + "\n\n");
var mid: Int = (size / 2).toInt;
var i: Int = 0;
var j: Int = 0;
while (i < size)
{
j = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1
|| (i <= mid && i == j)
|| (i < mid && size - 1 - i == j))
{
print(" *");
}
else
{
print(" ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
}
}
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
/*
Swift Program
Print M pattern
*/
class MyPattern
{
func print_m(_ size: Int)
{
if (size <= 2 || size % 2 == 0)
{
return;
}
print("\nHeight : ", size ,"\n\n", terminator: "");
let mid: Int = size / 2;
var i: Int = 0;
var j: Int = 0;
while (i < size)
{
j = 0;
while (j < size)
{
//Test case which is printing the value of star
if (j == 0 || j == size - 1 || (i <= mid && i == j) || (i < mid && size - 1 - i == j))
{
print(" *", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Simple test
obj.print_m(5);
obj.print_m(9);
obj.print_m(13);
}
main();
Output
Height : 5
* *
* * * *
* * *
* *
* *
Height : 9
* *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
Height : 13
* *
* * * *
* * * *
* * * *
* * * *
* * * *
* * *
* *
* *
* *
* *
* *
* *
Time Complexity
The time complexity of the code is O(n^2), where n is the size of the pattern. This is because we use nested loops to iterate over each position in the pattern, resulting in quadratic time complexity.
Finally
In this article, we have learned how to print the M pattern using a C program. We discussed the problem statement, provided examples, explained the algorithm and pseudocode, and analyzed the time complexity of the code. The M pattern can be a fun and challenging exercise to improve your programming skills. You can further modify the program to handle different patterns or add more complexity. Happy coding!
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