title: Multiplying 2 Matrices - Output Issue
body: <p>I'm writing a program that multiplies 2 matrices using 2D Arrays. Then the results should be printed in a rectangular form. Here's the code:</p>
  
  <pre><code>import java.util.Scanner;
  public class MatrixMath   
  {
  
  
  
  public static void main(String[] args) {
     int m, n, p, q, c, d, k;
     int sum1 = 0;
  
     Scanner scan = new Scanner(System.in);  
  
     System.out.println("Please enter the number of rows and columns for the first       matrix."); 
        m = scan.nextInt();
        n = scan.nextInt();
  
     int first[][] = new int[m][n];
  
          System.out.println("Enter the elements of first matrix:"); 
  
          for ( c = 0 ; c &lt; m ; c++ ) 
             for ( d = 0 ; d &lt; n ; d++ )
                first[c][d] = scan.nextInt();
  
     System.out.println("Please enter the number of rows and columns for the second matrix."); 
        p = scan.nextInt();
        q = scan.nextInt();
  
  
       int second[][] = new int[p][q];
       int multiply[][] = new int[m][q];
  
       System.out.println("Enter the elements of second matrix:"); 
  
       for ( c = 0 ; c &lt; p ; c++ )   
          for ( d = 0 ; d &lt; q ; d++ )
             second[c][d] = scan.nextInt();
  
  
       for ( c = 0 ; c &lt; m ; c++ )  
       {
          for ( d = 0 ; d &lt; q ; d++ )
          {   
             for ( k = 0 ; k &lt; p ; k++ )
             {
             sum1 = sum1 + first[c][k]*second[k][d]; 
             }
  
             multiply[c][d] = sum1;
  
             sum1 = 0;
             }
          }
  
             System.out.println("The product of the two matrices is: ");  
  
                for ( c = 0 ; c &lt; m ; c++ )
                {
                   for ( d = 0 ; d &lt; q ; d++ )
                   System.out.print(multiply[c][d]+"\t");
  
                   System.out.print("\n");
  
  
             int sum[][] = new int[m][n];  // Sum is calculated.
  
             for ( c = 0 ; c &lt; m ; c++ )  
                   for ( d = 0 ; d &lt; n ; d++ )
  
  
  
  
       System.out.println("The sum of the two matrices is: "); 
       for ( c = 0 ; c &lt; m ; c++ )
             {
             for ( d = 0 ; d &lt; n ; d++ )
             System.out.print(sum[c][d]+"\t");
  
             System.out.println();
  
     }
    }
   }
  }
  </code></pre>
  
  <p>Output issue after the user enters values:</p>
  
  <p><strong>The product of the two matrices is:<br>
  8        8<br>
  The sum of the two matrices is:<br>
  The sum of the two matrices is:<br>
  The sum of the two matrices is:<br>
  The sum of the two matrices is:<br>
  0   0<br>
  0   0</strong> </p>
  
  <p>I want the sum printed as a rectangle, as well as the product... but only half the product seems to be printing and something is wrong with the sum output. Help is appreciated. Thanks very much.</p>