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 indexj
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:
-
- Copy the Java program code into a text file and save it with a
.java
extension (e.g.,ChessboardPattern.java
). - Open your terminal or command prompt and navigate to the folder where the program file is saved.
- Compile the program using the Java compiler:
- Copy the Java program code into a text file and save it with a
javac ChessboardPattern.java
-
- After successful compilation, run the program with the following command:
java ChessboardPattern
- Enter the desired size of the chessboard when prompted, and the program will display the chessboard pattern on the screen.