Computer Science, asked by BrainlyProgrammer, 1 month ago

[Super Challenge]
#CS

Question :--
Create your own program to accept two matrics as input and find the product of both the matric. Check if matric multiplication is possible or not (Matric can be of any order).

Hint:-
• You must remember how to multiply matric multiplication :D

••Sample I/O in attachment​

Attachments:

Answers

Answered by anindyaadhikari13
23

THE CO‎‎‎‎DE.

import java.util.*;

public class MatrixMultiplication{

   public static void main(String s[]){

       Scanner sc=new Scanner(System.in);

       int a,b,c,d,i,j,k;

       System.out.print("Enter the number of rows for the first matrix: ");

       a=sc.nextInt();

       System.out.print("Enter the number of cols for the first matrix: ");

       b=sc.nextInt();

       int A[][]=new int[a][b];

       System.out.println("Enter the matrix elements..");

       for(i=0;i<a;i++){

           for(j=0;j<b;j++){

               System.out.printf("A[%d][%d] > ",i,j);

               A[i][j]=sc.nextInt();

           }

       }

       System.out.println("-------------------------------------------\n");

       System.out.print("Enter the number of rows for the second matrix: ");

       c=sc.nextInt();

       System.out.print("Enter the number of cols for the second matrix: ");

       d=sc.nextInt();

       int B[][]=new int[c][d];

       System.out.println("Enter the matrix elements..");

       for(i=0;i<c;i++){

           for(j=0;j<d;j++){

               System.out.printf("B[%d][%d] > ",i,j);

               B[i][j]=sc.nextInt();

           }

       }

       if(b!=c){

           System.out.println("Matrix Multiplication is not possible. Please enter valid matrix.");

       }

       else{

           int C[][]=new int[a][d];

           for(i=0;i<a;i++){

               for(j=0;j<d;j++){

                   for(k=0;k<b;k++)

                       C[i][j]+=A[i][k]*B[k][j];

               }

           }  

           System.out.println("Matrix A: ");

           displayMatrix(A);

           System.out.println("Matrix B: ");

           displayMatrix(B);

           System.out.println("Their product: ");

           displayMatrix(C);

       }

   }  

   static void displayMatrix(int a[][]){

       for(int x[]:a){

           for(int y:x)

               System.out.print(y+" ");

           System.out.println();

       }

       System.out.println();

   }

}

\rule{300}{2}

Note: Matrix multiplication is possible only when the number of columns of the first matrix is equal to the number of rows of the second matrix.

Here, we have checked whether matrix multiplication is possible or not using if-else statement.

We can also check this using try-catch block because when matrix multiplication is not possible, Array index out of bounds exception is thrown. In that case, we can write a message on the catch block that matrix multiplication is not possible.

See attachment for output.

Attachments:

anindyaadhikari13: Thanks for the brainliest ^_^
Similar questions