Creating a boolean array by prime number

Write a code fragment that creates an N-by-N Boolean array a[][] such that a[i][j] is true if i and j are relatively prime and false otherwise.

My code is

public static void main(String[] args){
    int N = 12;
    boolean[][] a = new boolean[N][N];

    for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a[i].length; j++){
            if(i%j == 0){
                a[i][j] = false;
            }else{
                a[i][j] = true;
            }
        }
    }

    printArray(a);
}


public static void printArray(boolean[][] a){
    for(int i=0; i<a.length; i++){
        for(int j=0; j< a[i].length; j++){
            System.out.printf(a[i][j]);
            System.out.printf("n");
        }
    }
}

The running exception is Exception in thread “main”

java.lang.ArithmeticException: / by zero
        at Array.main(Array.java:8)

How could I fix my code?

Source: java

Leave a Reply