The letter M star pattern draws two upright posts and a V between them that dips to the middle of the letter, the way a capital M is printed.
Quick answer: Loop over 7 rows and 5 columns. Print the two posts in the first and last column. On the top three rows, also print the two diagonals of the V: j == i from the left and j == n โ 1 โ i from the right. They meet in the centre on row 3.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter M Star Pattern output
Output (7 rows):
* * ** ** * * * * * * * * * * *

How the letter m star pattern works
The two diagonals start in the top corners and move one column towards the middle on each row. With a width of 5 they meet in column 3 on row 3, so the V is drawn only while i โค 3. Below that, the posts carry on alone. Every letter on this site is drawn on a grid of rows and columns, and each stroke is one simple test on i and j.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
n โ 2positions in the row. Print stars wherej = 1 or j = n โ 2 or (i โค (n โ 1) / 2 and (j = i or j = n โ 1 โ i)); print spaces everywhere else. - Move to the next line.
Key takeaways
- An M is two posts plus a V in the top half.
- The diagonals are
j == iandj == n โ 1 โ ifor a 5-wide letter. - Limiting the V to the top rows stops it from crossing the posts lower down.
- Turn the condition upside down and you get a W.
Letter M Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if (j == 1 || j == n - 2 || (i <= (n - 1) / 2 && (j == i || j == n - 1 - i)))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter M Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if (j == 1 || j == n - 2 || (i <= (n - 1) / 2 && (j == i || j == n - 1 - i)))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter M Star Pattern in Java
public class LetterMStar {
public static void main(String[] args) {
int n = 7;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - 2; j++) {
if (j == 1 || j == n - 2 || (i <= (n - 1) / 2 && (j == i || j == n - 1 - i)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter M Star Pattern in Python
n = 7
for i in range(1, n + 1):
for j in range(1, n - 2 + 1):
if j == 1 or j == n - 2 or (i <= (n - 1) // 2 and (j == i or j == n - 1 - i)):
print("*", end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nยฒ) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I print a W?
Use the same posts and draw the V in the bottom rows instead: replace i with n + 1 โ i in the diagonal tests.
Why does the V stop at row 3?
With a width of 5 the two diagonals meet in column 3 on row 3. Going further would draw an X across the letter.
Can I make the M wider?
Yes, but the V must still meet in the middle: for a width w the diagonals are j == i and j == w + 1 โ i, drawn while i โค (w + 1) / 2.








