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

Number 8 Star Pattern

Read summary with AI tools

The number 8 star pattern draws the digit 8 with stars: three short bars across the top, middle and bottom, with straight sides between them. It is a small step towards printing digits and letters as star art.

Quick answer: Use 2n − 1 rows of n columns. On the three bar rows (1, n and 2n − 1) print stars in the inner columns 2 to n − 1. On every other row print stars only in the first and last column. The missing corners make the 8 look rounded.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the number 8 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

Number 8 Star Pattern output

Output (n = 5 (9 rows)):

 ***
*   *
*   *
*   *
 ***
*   *
*   *
*   *
 ***
Number 8 Star Pattern layout diagram, row by row (n = 5 (9 rows))
How each row of the number 8 star pattern is built (n = 5 (9 rows)). Blue cells are printed characters; grey dots are spaces.

How the number 8 star pattern works

Split the digit into horizontal bars and vertical sides. Bars sit on three rows and leave out the two corner columns; sides fill the corner columns on all the other rows. Leaving the corners empty is the trick that turns two stacked rectangles into a rounded 8. The middle bar is shared by the top and bottom loops of the 8.

  1. Run the outer loop for i from 1 to 2 × n − 1, one row per pass.
  2. Go through n positions in the row. Print stars where ((i = 1 or i = n or i = 2 × n − 1) and j > 1 and j < n) or (i ≠ 1 and i ≠ n and i ≠ 2 × n − 1 and (j = 1 or j = n)); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • An 8 is three bars and four short sides.
  • Bars skip the corner columns, which rounds off the shape.
  • The middle bar on row n is shared by both loops of the 8.
  • The same bar-and-side idea can draw 0, 3, 6 and 9.

Number 8 Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= 2 * n - 1; i++) {
        for (int j = 1; j <= n; j++) {
            if (((i == 1 || i == n || i == 2 * n - 1) && j > 1 && j < n) || (i != 1 && i != n && i != 2 * n - 1 && (j == 1 || j == n)))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Number 8 Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= 2 * n - 1; i++) {
        for (int j = 1; j <= n; j++) {
            if (((i == 1 || i == n || i == 2 * n - 1) && j > 1 && j < n) || (i != 1 && i != n && i != 2 * n - 1 && (j == 1 || j == n)))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Number 8 Star Pattern in Java

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

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

Number 8 Star Pattern in Python

n = 5

for i in range(1, 2 * n):
    for j in range(1, n + 1):
        if ((i == 1 or i == n or i == 2 * n - 1) and j > 1 and j < n) or (i != 1 and i != n and i != 2 * n - 1 and (j == 1 or j == n)):
            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 a 0 instead?

Keep the top and bottom bars and the two sides, and drop the middle bar. The middle row then prints the two side stars like the other rows.

Why does n control both the width and the height?

It keeps the digit in proportion: n columns wide and 2n − 1 rows tall. Use separate variables if you want a narrower or wider 8.

How do I print several digits next to each other?

Loop over the rows once, and for each row print the row of every digit in turn with a space between them. Each digit needs its own condition, so a small function per digit keeps the code tidy.

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.