The number and star checker grid alternates numbers and stars like a chessboard. Wherever a number appears it is i + j − 1, so the numbers along each diagonal are the same: 1, then 3, then 5, 7 and 9.
Quick answer: Loop over an n × n grid. When (i + j) is even, print the number i + j − 1; otherwise print a star. Every printed number is odd, because i + j is even there, and the numbers repeat along each diagonal.
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Number and Star Checker Grid output
Output (n = 5):
1 * 3 * 5 * 3 * 5 * 3 * 5 * 7 * 5 * 7 * 5 * 7 * 9

Same shape, other characters: the Number Frame Layers Pattern, Square Star Pattern and String Plus Pattern programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the number and star checker grid works
The checker layout comes from the remainder of i + j, just like the checkerboard pattern. The number is chosen from the same sum, so every cell on a diagonal from bottom left to top right shows the same value. Starting at 1 in the corner and adding 2 per diagonal gives 1, 3, 5, 7, 9.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
npositions in the row. Print numbers (valuei + j − 1) where(i + j) % 2 = 0; print stars everywhere else. - Move to the next line.
Key takeaways
(i + j) % 2decides number or star.- The value
i + j − 1is constant along each anti-diagonal. - All printed numbers are odd, because they only appear where
i + jis even. - With
n = 5the largest number is 9, in the bottom-right corner.
Number and Star Checker Grid in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0)
printf("%d ", i + j - 1);
else
printf("* ");
}
printf("\n");
}
return 0;
}Number and Star Checker Grid in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0)
cout << i + j - 1 << " ";
else
cout << "* ";
}
cout << endl;
}
return 0;
}Number and Star Checker Grid in Java
public class NumberStarCheckerGrid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0)
System.out.print((i + j - 1) + " ");
else
System.out.print("* ");
}
System.out.println();
}
}
}Number and Star Checker Grid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
if (i + j) % 2 == 0:
print(i + j - 1, 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
Why are all the numbers odd?
Numbers only appear where i + j is even, and an even number minus 1 is always odd.
How do I put stars in the corner instead?
Swap the two branches: print the number when (i + j) is odd. The top-left cell then becomes a star.
Why does the grid stay aligned?
Every cell prints two characters, a number or star followed by a space. Above 9 the numbers take three characters, so print them in a fixed width for larger grids.






