Matrix subtraction in java

Program Description

Matrix subtraction in java. This program shows an implementation of matrix subtraction 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 subtraction is possible and performs subtraction if it is valid and prints out the resultant matrix.If the matrix subtraction is not valid, the program gives an error message.

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




import java.util.Scanner;

class matrix_sub {
    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();
		}
		
      	   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 sub_mat() method to perform addition of matrices
          sub_mat(mat1,mat2);
		  
    }
	
	
	/*sub_mat function description
	
	Function: sub_mat().Checks whether matrix subtraction is valid for two given matrices
	          and gives the result. If the subtraction 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 sub_mat(int x[][],int y[][])
    {
	     //checking for validity of matrix subtraction
  	     if((x.length == y.length) && (x[0].length == y[0].length))
  	     {
  	    	 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 subtraction of matrices is not valid
  	     else
  	    	 System.out.println("Subtraction 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 : 1
Enter the mat1[0][1] element : 2
Enter the mat1[0][2] element : 3
Enter the mat1[1][0] element : 4
Enter the mat1[1][1] element : 5
Enter the mat1[1][2] element : 6
Enter the mat1[2][0] element : 7
Enter the mat1[2][1] element : 8
Enter the mat1[2][2] element : 9
Matrix 1 is :
1 2 3
4 5 6
7 8 9

Matrix 2 is :
1 2 3
4 5 6
7 8 9
Resultant matrix
0 0 0
0 0 0
0 0 0


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 : 2
Matrix 1 is :
1

Matrix 2 is :
1 2
Subtraction cannot be performed

Comments
comments powered by Disqus