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;
                a[i][j] = true;


public static void printArray(boolean[][] a){
    for(int i=0; i<a.length; i++){
        for(int j=0; j< a[i].length; j++){

The running exception is Exception in thread “main”

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

How could I fix my code?

Source: java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.