Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading