Java

 

Introduction

A chessboard pattern consists of alternating black and white squares arranged in a grid. This pattern is commonly used in games like chess and can be easily generated using loops in Java programming. In this guide, you will learn how to create a chessboard pattern program that outputs the pattern using simple Java code.

Objective

The objective of this program is to display a chessboard pattern of any size using the Java programming language. The chessboard will have alternating rows and columns of characters representing black and white squares. This is achieved by using loops to generate a 2D grid where each cell alternates between two characters.

Code

public class ChessboardPattern {

    // Function to print the chessboard pattern
    public static void printChessboard(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // Check if the sum of row and column indices is even or odd
                if ((i + j) % 2 == 0) {
                    System.out.print("X ");  // Representing the black square
                } else {
                    System.out.print("O ");  // Representing the white square
                }
            }
            System.out.println();  // Move to the next line after each row
        }
    }

    public static void main(String[] args) {
        int n;

        // Ask the user for the size of the chessboard
        System.out.print("Enter the size of the chessboard (e.g., 8 for an 8x8 board): ");
        java.util.Scanner scanner = new java.util.Scanner(System.in);
        n = scanner.nextInt();

        // Call the function to print the chessboard
        printChessboard(n);
        scanner.close();
    }
}

Explanation of the Program

This Java program generates a chessboard pattern using nested loops to print alternating black and white squares. Here’s a breakdown of how the program works:

  • printChessboard function: This function is responsible for printing the chessboard pattern. It takes an integer n as input, representing the size of the chessboard (e.g., 8×8, 10×10, etc.).
  • Nested for loops: The outer loop iterates over the rows of the chessboard, while the inner loop iterates over the columns. The combination of the two loops ensures every cell in the grid is processed.
  • Alternating pattern: Inside the loops, the program checks if the sum of the row index i and the column index j is even or odd. If the sum is even, it prints ‘X’ (representing a black square); otherwise, it prints ‘O’ (representing a white square). This alternating pattern creates the chessboard effect.
  • Input: The program asks the user to input the size of the chessboard (n), which determines how many rows and columns the chessboard will have.
  • Output: The program displays the chessboard pattern, where ‘X’ and ‘O’ represent black and white squares, respectively.

How to Run the Program

Follow these steps to run the program on your computer:

    1. Copy the Java program code into a text file and save it with a .java extension (e.g., ChessboardPattern.java).
    2. Open your terminal or command prompt and navigate to the folder where the program file is saved.
    3. Compile the program using the Java compiler:
javac ChessboardPattern.java
    1. After successful compilation, run the program with the following command:
java ChessboardPattern
  1. Enter the desired size of the chessboard when prompted, and the program will display the chessboard pattern on the screen.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)