Matrix addition in java

Program Description

Matrix addition in java. This program shows an implementation of matrix addition in java.

The program takes input for the 2D matrix from the user. There is no limit on the matrix size.The user can take any number of rows and columns. The program then checks,if the addition is possible and performs addition if it is valid and prints out the resultant matrix.If the matrix addition is not valid, the program gives an error message.

Program
/*********************************
*   Matrix addition in java
*
*	code.cheraus.com
*********************************/

import java.util.Scanner;

class matrix_add {
                  public static void main(String args[])
                  {
                      Scanner num = new Scanner(System.in);
                    
					  //taking matrix dimensions from the user
                      System.out.print("Enter number of rows in first matrix : ");
                      int r1 = num.nextInt();
                      System.out.print("Enter number of columns in first matrix : ");
                      int c1 = num.nextInt();
					  
					  //declaring matrix
                      int mat1[][] = new int[r1][c1];
					  
					  
					  //taking matrix data from user
                      System.out.println("Enter the first matrix");
                      for(int i=0;i<r1;i++)
					    {
                    	  for(int j=0;j<c1;j++)
                    	  {
                    		  System.out.print("Enter the mat["+ i+"]["+ j +"] element : ");
                    		  mat1[i][j] = num.nextInt();
                    	  }
						}   
                      
					  
					  //taking input for second matrix
                      System.out.print("Enter number of rows in second matrix : ");
                      int r2 = num.nextInt();
                      System.out.print("Enter number of columns in second matrix : ");
                      int c2 = num.nextInt();
					  
                      int mat2[][] = new int[r2][c2];
					  
                      System.out.println("Enter the second matrix");
					  
                      for(int i=0;i<r2;i++)
					    {
                    	  for(int j=0;j<c2;j++)
                    	  {
                    		  System.out.print("Enter the mat1["+ i+"]["+ j +"] element : ");
                    		  mat2[i][j] = num.nextInt();
                    	  }
						}
						
						
					//printing the first matrix
                      System.out.println("Matrix 1 is : ");
                      for(int i=0;i<mat1.length;i++)
					  {
                    	  for(int j=0;j<mat1[0].length;j++)
                    	  {
                    		  System.out.print(mat1[i][j] + "\t");
                    		 
                    	  }
                    	   System.out.println();
                      }
                      
                      
					  //printing second matrix
					  System.out.println("Matrix 2 is : ");
                      for(int i=0;i<mat2.length;i++)
					  {
                    	  for(int j=0;j<mat2[0].length;j++)
                    	  {
                    		  System.out.print(mat2[i][j] + "\t");
                    		 
                    	  }
                    	   System.out.println();
                      }
					  
					  //calling add_mat() method to perform addition of matrices
                        add_mat(mat1,mat2);
                  }
				  
				  
	/*add_mat function description
	
	Function: add_mat().Checks whether matrix addition is valid for two given matrices
	          and gives the result. If the addition is possible, it prints the
              resultant matrix, else it prints an error message.
	Input: Two integer matrices.		  
	Return: The return type is void.		  
	
	*/

      public static void add_mat(int x[][],int y[][])
       {
		 //checking for validity of matrix addition
          if((x.length == y.length) && (x[0].length == y[0].length))
            {
		     //if addition is valid,perform addition
           	 int res[][] = new int[x.length][x[0].length];
           	 for(int i=0;i<x.length;i++)
		     {
               for(int j=0;j<x[0].length;j++)
               {
               	res[i][j] = x[i][j] + y[i][j];
                }
			}

            
			 //printing the resultant matrix							  
             System.out.println("Resultant  matrix");
             for(int i=0;i<x.length;i++)
			 {
           	  for(int j=0;j<x[0].length;j++)
           	  {
            	  System.out.print(res[i][j] + "\t");
                             		 
              }
             
			 System.out.println();
        }
     }
                	     
   //if addition of matrices is not valid
	else
     System.out.println("Addition cannot be performed");
                	
                  }
}

Output
Test Case 1:

Enter number of rows in first matrix : 3
Enter number of columns in first matrix : 3
Enter the first matrix
Enter the mat[0][0] element : 1
Enter the mat[0][1] element : 2
Enter the mat[0][2] element : 3
Enter the mat[1][0] element : 4
Enter the mat[1][1] element : 5
Enter the mat[1][2] element : 6
Enter the mat[2][0] element : 7
Enter the mat[2][1] element : 8
Enter the mat[2][2] element : 9
Enter number of rows in second matrix : 3
Enter number of columns in second matrix : 3
Enter the second matrix
Enter the mat1[0][0] element : 9
Enter the mat1[0][1] element : 8
Enter the mat1[0][2] element : 7
Enter the mat1[1][0] element : 6
Enter the mat1[1][1] element : 5
Enter the mat1[1][2] element : 4
Enter the mat1[2][0] element : 3
Enter the mat1[2][1] element : 2
Enter the mat1[2][2] element : 1
Matrix 1 is :
1 2 3
4 5 6
7 8 9
Matrix 2 is :
9 8 7
6 5 4
3 2 1
Resultant matrix
10 10 10
10 10 10
10 10 10


Test Case 2:

Enter number of rows in first matrix : 1
Enter number of columns in first matrix : 1
Enter the first matrix
Enter the mat[0][0] element : 1
Enter number of rows in second matrix : 1
Enter number of columns in second matrix : 2
Enter the second matrix
Enter the mat1[0][0] element : 1
Enter the mat1[0][1] element : 1
Matrix 1 is :
1
Matrix 2 is :
1 1
Addition cannot be performed

Comments
comments powered by Disqus