New: 206 pattern programs with tested code in C, C++, Java and Python Explore patterns

Letter M Star Pattern

Read summary with AI tools

Free

One click: get a short summary of this page and the code explained in simple words.

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
  1. Output
  2. How the letter m star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Letter M Star Pattern output

Output (7 rows):

*   *
** **
* * *
*   *
*   *
*   *
*   *
Letter M Star Pattern layout diagram, row by row (7 rows)
How each row of the letter m star pattern is built (7 rows). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n โˆ’ 2 positions in the row. Print stars where j = 1 or j = n โˆ’ 2 or (i โ‰ค (n โˆ’ 1) / 2 and (j = i or j = n โˆ’ 1 โˆ’ i)); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • An M is two posts plus a V in the top half.
  • The diagonals are j == i and j == n โˆ’ 1 โˆ’ i for 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.

Still stuck? Ask AI to explain this code

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.