mercredi 13 novembre 2019

how to print this loop pattern

How to solve this pattern? its looks like a wave for me.

input:4

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

i managed to pull out some correct numbers but i know its very far from solved.

Here is my code

//asume g is from input
 int g = 4;

      for(int x = 1; x <=g;x++){
      int d = x;
        for(int y = 1;y<=g;y++){
          System.out.print(d+" ");
          d = g*x-y;
        }
        System. out. println();
      }

output:

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

Aucun commentaire:

Enregistrer un commentaire