Array-promo
Matrix-promo

Question

Program to determine whether two matrices are equal

				
					MATRIX A:[1, 0, 0]
         [0, 1, 0]
         [0, 0, 1]

MATRIX B:[1, 0, 0]
         [0, 1, 0]
         [0, 0, 1]
				
			

BOTH MATRICES ARE EQUAL

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class equalMatrix  
{  
    public static void main(String[] args)
    {  
        int row=0, col=0,rowb=0,colb=0,i=0,j=0;  
        int a[][],b[][];
        boolean flag=true;
        
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE ROW OF MATRIX A");
        row=sc.nextInt();
        System.out.println("ENTER THE COLUMN OF MATRIX A");
        col=sc.nextInt();
        System.out.println("ENTER THE ROW OF MATRIX B");
        rowb=sc.nextInt();
        System.out.println("ENTER THE COLUMN OF MATRIX B");
        colb=sc.nextInt();
        
        if(row!=rowb||col!=colb)
        {
            System.out.println("BOTH MATRICES ARE NOT EQUAL");
        }
        
        else
        {
            a=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();
                }
            }
            
            b=new int[rowb][colb];
            System.out.println("ENTER THE ELEMENTS IN MATRIX B");
            for(i=0;i< rowb;i++)
            {
                for(j=0;j< colb;j++)
                {
                    b[i][j]=sc.nextInt();
                }
            }
        
            
            for(i=0;i< row;i++)
            {
                for(j=0;j< col;j++)
                {
                    if(a[i][j]!=b[i][j])
                    {
                        flag=false;
                        break;
                    }
                    
                }
            }
            if(flag==false)
            {
                System.out.println("TWO MATRICES ARE NOT EQUAL");
            }
            else
            {
               System.out.println("TWO MATRICES ARE EQUAL");
            }
        }
        
    }  
}


				
			

Coding Store

Leave a Reply

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