Array-promo
Matrix-promo

Question

Program to subtract  two matrices

				
					Matrix a = [4, 5, 5]
           [3, 0, 1]
           [1, 2, 3]

Matrix b = [2, 0, 3]
           [2, 3, 1]
           [0, 1, 1]
           
Subtraction of two matrices:
[2  5  2]
[1 -3 0]
[1  1  2]
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SubtractTwoMatrix  
{  
    public static void main(String[] args)
    {  
        int row=0, col=0,i=0,j=0;  
        int a[][],b[][],subtract[][];
        
        
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE ROW OF MATRIX");
        row=sc.nextInt();
        System.out.println("ENTER THE COLUMN OF MATRIX");
        col=sc.nextInt();
        
        a=new int[row][col];
        b=new int[row][col];
    
        System.out.println("ENTER THE ELEMENTS IN MATRIX A");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                a[i][j]=sc.nextInt();
            }
        }
        
        System.out.println("ENTER THE ELEMENTS IN MATRIX B");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                b[i][j]=sc.nextInt();
            }
        }
        
        subtract=new int [row][col];
        
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                subtract[i][j]=a[i][j]-b[i][j];
            }
        }
        
        System.out.println("MATRIX A - MATRIX B");
        for(i=0;i< row;i++)
        {
            for(j=0;j< col;j++)
            {
                System.out.print(subtract[i][j]+" ");
            }
            System.out.println();
        }  
        
    }  
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *