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

Letter N Star Pattern

The letter N star pattern draws two upright posts and one diagonal that runs from the top left to the bottom right. The grid is square, so the diagonal lands exactly in the corners.

Quick answer: Loop over a 7 ร— 7 grid. Print a star in the first column, the last column, and on the main diagonal, j == i. That one diagonal, running from the top-left corner to the bottom-right corner, is what turns two posts into an N.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the letter n 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 N Star Pattern output

Output (7 rows):

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

How the letter n star pattern works

On the main diagonal the row and column numbers are equal, so the stroke starts in the top-left corner and finishes in the bottom-right one. The grid must be square for that to happen, which is why this letter uses n columns. 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 positions in the row. Print stars where j = 1 or j = n or j = i; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The diagonal of an N is j == i.
  • A square grid makes the diagonal end in the corners.
  • Use j == n โˆ’ i + 1 instead and you get a mirrored N, which looks like the Cyrillic ะ˜.
  • It is the same diagonal test used in the X (cross) pattern.

Letter N 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; j++) {
            if (j == 1 || j == n || j == i)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Letter N 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; j++) {
            if (j == 1 || j == n || j == i)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Letter N Star Pattern in Java

public class LetterNStar {
    public static void main(String[] args) {
        int n = 7;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (j == 1 || j == n || j == i)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Letter N Star Pattern in Python

n = 7

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if j == 1 or j == n or j == 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 draw the letter Z with the same idea?

Swap the posts for top and bottom rows and use the other diagonal: i == 1 || i == n || j == n โˆ’ i + 1. See the letter Z star pattern.

Why is the N wider than the other letters?

The diagonal needs as many columns as rows to stay straight. A 5-wide N with 7 rows would need a staircase line.

How do I let the user choose the size?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 48 of 48 in Star Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.