title: how to print magic matrix upto n square for a given number n
body: <p>I am trying to implement matrix logic in a program riddle. This is not a home work I am just trying to practice it on my own but could go further without guidance or approach. </p>
  
  <p>The problem is suppose we have given number let's say n than a matrix should get generated upto n*n numbers with following pattern</p>
  
  <p>ex: Number is 3</p>
  
  <p>1 2 3</p>
  
  <p>7 8 9 </p>
  
  <p>4 5 6</p>
  
  <p>like wise if the the number is 4
  than</p>
  
  <p>1 2 3 4</p>
  
  <p>9 10 11 12</p>
  
  <p>13 14 15 16</p>
  
  <p>5 6 7 8 </p>
  
  <p>for problem solving I used following logic but it could not give me expected output</p>
  
  <pre><code>public static void printPattern(int i){
      int num= i*i;
      int n=1;
      int[][] matrixArray = new int[i][i];
  
      for (int g=0;g&lt;i ;g++ ){
  
          for (int j=0; j&lt;i&amp;&amp; n&lt;=num; j++,n++){
              System.out.println("i::"+i+"::j"+j+"number is :"+n);
              if (g!=0 &amp;&amp;g%2==0){
                  System.out.println("g is "+g);
                  matrixArray[g][j]=n;
              }
              else{
              matrixArray[g][j]=n;
              }
          }
      }
  
      for (int g=0;g&lt;i ;g++ ) {
  
          for (int j = 0; j &lt; i; j++) {
              System.out.print(matrixArray[g][j] + " ");
          }
          System.out.println();
      }
   }
  }
  </code></pre>