How not to print the same number for every row, I want different number at every position

I want to print numbers from 100000 to 1000000 separated by two spaces each side of the number. I want to print 15 columns/ 15 unique numbers per line but for each line it’s printing same number. How to fix this

class practicex{

public static void main(String[] args) {
    pract();
}

static void pract(){

    for (long i=100000; i<1000000; i++){

    for(long q=0; q<15; q++){
        System.out.print("  " + i + "  ");
    }
    System.out.println();
}
}

}

enter image description here

>Solution :

You are using the wrong loop. i is not changing. What you want to do is to have a combination of while -> for:

static void pract(){
    int i = 100000;
    while(i<1000001){
        for(int q=0; q<15; q++){
            System.out.print("  " + i + "  ");
            i++;
        }
        System.out.println();
    }
}

Leave a Reply