title: Print all the combinations of elements in matrix of size m * n
body: <h2>Print all the combinations of elements in matrix of size m * n</h2>
  
  <h3>Sample Example:</h3>
  
  <pre><code>1  3  5  
  2  6  7
  </code></pre>
  
  <h3>Expected Output:</h3>
  
  <pre><code>1 , 3 , 5 
  1 , 3 , 7 
  1 , 6 , 5
  1 , 6 , 7 
  2 , 3 , 5
  2 , 3 , 7
  2 , 6 , 5
  2 , 6 , 7
  </code></pre>
  
  <h3>Rules:</h3>
  
  <ul>
  <li>Every combination starts from left of matrix and proceeds towards right. It may switch rows though.</li>
  <li>Every combination should have number of elements equal to number of columns.</li>
  <li>A combination can't have an element from the same column present twice.</li>
  <li><p>Number of columns and rows could vary. So solution has to be generic.</p>
  
  <pre><code>import java.util.Scanner;
  
  class Combination {
  public static void main(String args[]) {
  int row, col, i, j;
  
  Scanner in = new Scanner(System.in);
  System.out.println("Enter the number of rows and columns of matrix:\n");
  row = in.nextInt();
  col = in.nextInt();
  
  int first[][] = new int[row][col];
  System.out.println("Enter the elements if matric m*n:\n");
  for (i = 0; i &lt; row; i++) {
      for (j = 0; j &lt; col; j++) {
          first[i][j] = in.nextInt();
      }
  }
  System.out.println("Matrix:\n");
  for (i = 0; i &lt; row; i++) {
      for (j = 0; j &lt; col; j++) {
          System.out.print(first[i][j] + "\t");
      }
      System.out.println();
  }
  // Final Logic from here...
  System.out.println("\nOut Matrix:\n");
  for (i = 0; i &lt; 2; i++) {
      for (j = 0; j &lt; 2; j++) {
          for (int k = 0; k &lt; 2; k++) {
              System.out.println(first[i][0] + "," + first[j][1] + ","
                      + first[k][2]+"\n");
          }
      }
  }
   /* while (i &lt; 2) {
      j = 0;
      while (j &lt; 2) {
          k = 0;
          while (k &lt; 2) {
              System.out.println(first[i][0] + "," + first[j][1] + ","
                      + first[k][2]);
              k++;
          }
          j++;
      }
      i++;
  }*/
  in.close();
  }
  }
  </code></pre></li>
  </ul>
  
  Works good for specific input but not able to do it dynamic....Need Help..
  
  <h2>Thanks in Advance......</h2>