Numeric Checkerboard Grid
This program prints a numeric checkerboard grid using nested for loop.
Problem Statement
Write a Java program that prints a 4x4 checkerboard grid of alternating 1s and 0s using nested for loop.
Source Code
| 1 | public class Checkerboard { |
| 2 | public static void main(String[] args) { |
| 3 | for(int i=0; i<4; i++) { |
| 4 | for(int j=0; j<4; j++) { |
| 5 | if((i+j) % 2 == 0) { |
| 6 | System.out.print("1 "); |
| 7 | } else { |
| 8 | System.out.print("0 "); |
| 9 | } |
| 10 | } |
| 11 | System.out.println(); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1
Explanation
The outer loop controls rows, inner loop prints alternating 1s and 0s based on (i+j) % 2. This demonstrates how nested loops can generate advanced numeric grids with alternating patterns. Shows how nested for loops can generate checkerboard grids.