vendredi 29 septembre 2017

i want to print the a number when the range is provided

i want the following pattern to be printed in java when we provide a no it print the following pattern here n = 4 output:

1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13

for (int i=0; i<n/2 ; i++) {
            for (int j=0;j<n ;j++ ) {
                k++;
                System.out.print(k+" ");

            }
            l=k+n;
            k = l;
            System.out.println();
            for (int s=0; s<n;s++ ) {
                System.out.print(l+" ");
                l--;
            }
            System.out.println();
        }
        if (n%2!=0) {
            for (int i=0;i<n ;i++ ) {
                k++;
                System.out.print(k+" ");

            }
        }

here is a code which print

1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13

Aucun commentaire:

Enregistrer un commentaire