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
Number 8 Star Pattern output
Output (n = 5 (9 rows)):
*** * * * * * * *** * * * * * * ***

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.
- Run the outer loop for
ifrom1to2 × n − 1, one row per pass. - Go through
npositions 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. - 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
nis 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.











