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

Alternating X and 0 Rectangles Pattern

The alternating X and 0 rectangles pattern fills a grid with nested rectangular rings that take turns: X on the border, 0 on the next ring, X again inside that. It is a well-known GeeksforGeeks matrix problem.

Quick answer: For each cell, take the distance to the nearest edge: the smallest of i, j, rows βˆ’ 1 βˆ’ i and cols βˆ’ 1 βˆ’ j. Print X when that distance is even and 0 when it is odd. Every ring has one distance, so the rings alternate automatically.

This program is part of the Spiral Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alternating x and 0 rectangles 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

Alternating X and 0 Rectangles Pattern output

Output (5 rows, 6 columns):

X X X X X X
X 0 0 0 0 X
X 0 X X 0 X
X 0 0 0 0 X
X X X X X X
Alternating X and 0 Rectangles Pattern layout diagram, row by row (5 rows, 6 columns)
How each row of the alternating x and 0 rectangles pattern is built (5 rows, 6 columns). Blue cells are printed characters; grey dots are spaces.

How the alternating x and 0 rectangles pattern works

The same distance-to-edge idea used for concentric squares works for rectangles, because it only looks at how far a cell is from each side. The outer ring is distance 0, the next one distance 1, and so on, so the parity of the distance decides the character. Unlike a square, a rectangle’s innermost ring is a line or a small block, which you can see in the middle row of the output.

  1. For each row i and column j (counted from 0), set d to the smallest of i, j, rows βˆ’ 1 βˆ’ i and cols βˆ’ 1 βˆ’ j.
  2. Print X if d is even, otherwise 0, each followed by a space.
  3. Move to the next line after each row.

Key takeaways

  • The distance to the nearest edge labels each ring.
  • Even rings are X and odd rings are 0.
  • The same code works for any rectangle size.
  • The innermost ring of a rectangle can be a single line of cells.

Alternating X and 0 Rectangles Pattern in C

#include <stdio.h>

int main(void) {
    int rows = 5, cols = 6;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int d = i;                              /* distance to the nearest edge */
            if (j < d) d = j;
            if (rows - 1 - i < d) d = rows - 1 - i;
            if (cols - 1 - j < d) d = cols - 1 - j;
            printf(d % 2 == 0 ? "X " : "0 ");
        }
        printf("\n");
    }
    return 0;
}

Alternating X and 0 Rectangles Pattern in C++

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int rows = 5, cols = 6;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int d = min(min(i, j), min(rows - 1 - i, cols - 1 - j));   // distance to the nearest edge
            cout << (d % 2 == 0 ? "X " : "0 ");
        }
        cout << endl;
    }
    return 0;
}

Alternating X and 0 Rectangles Pattern in Java

public class AlternatingXORectangles {
    public static void main(String[] args) {
        int rows = 5, cols = 6;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int d = Math.min(Math.min(i, j), Math.min(rows - 1 - i, cols - 1 - j));   // distance to the nearest edge
                System.out.print(d % 2 == 0 ? "X " : "0 ");
            }
            System.out.println();
        }
    }
}

Alternating X and 0 Rectangles Pattern in Python

rows, cols = 5, 6

for i in range(rows):
    for j in range(cols):
        d = min(i, j, rows - 1 - i, cols - 1 - j)   # distance to the nearest edge
        print("X" if d % 2 == 0 else "0", end=" ")
    print()

Time and space complexity

Each cell takes constant work, so the program runs in O(rows Γ— cols) time with O(1) extra memory.

Frequently asked questions

Does it work for a square?

Yes. Set rows and cols to the same value and the rings become squares.

Can I build it by filling rings one by one instead?

Yes: fill the border with X, then the next ring with 0, moving four boundaries inwards each time, the way the spiral programs do. The distance formula avoids the extra loops.

Why is the middle row X X in the centre?

With 5 rows and 6 columns the innermost ring has distance 2, which is even, and it is only two cells long.

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 14 of 14 in Spiral 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.