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

Number and Star Checker Grid (1 * 3 * 5)

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
  1. Number and Star Checker Grid output
  2. How the number and star checker grid works
  3. Number and Star Checker Grid in C
  4. Number and Star Checker Grid in C++
  5. Number and Star Checker Grid in Java
  6. Number and Star Checker Grid in Python
  7. Time and space complexity
  8. Frequently asked questions

Number and Star Checker Grid output

Output (n = 5):

1 * 3 * 5
* 3 * 5 *
3 * 5 * 7
* 5 * 7 *
5 * 7 * 9
Number and Star Checker Grid layout diagram, row by row (n = 5)
How each row of the number and star checker grid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n positions in the row. Print numbers (value i + j − 1) where (i + j) % 2 = 0; print stars everywhere else.
  3. Move to the next line.

Key takeaways

  • (i + j) % 2 decides number or star.
  • The value i + j − 1 is constant along each anti-diagonal.
  • All printed numbers are odd, because they only appear where i + j is even.
  • With n = 5 the 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.

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 15 of 17 in Symbol 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.