Vous êtes sur la page 1sur 4

Question:

Write a program to input two 2Dimensional arrays and check whether their
multiplication is possible. If yes, print the product; if not, print a message of the
multiplication not being possible.

Algorithm:
1. Start.
2. The row and column values of the first matrix is taken as r1 and c1 respectively.
3. The row and column values of the second matrix is taken as r2 and c2 respectively
4. If c1 equals r2, the matrix multiplication is possible.
5. If c1 does not equal r2, a message is printed of the multiplication not being possible.
Go to step
6. If c1 equals value in r2, values are taken in the two 2D arrays respectively.
7. The original two matrices are displayed separately for the user.
8. Another 2D array is made with number of rows as r1 and number of columns as
c2.
9. Three loops are run.
i. The first loop for getting values from the first Matrix
ii. The second loop for getting values from the second Matrix, storing and
printing values from third matrix
iii. The third loop for performing the matrix multiplication.
10. Stop.
Source Code:
import java.util.*;
class MatrixMulti
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter row for Matrix 1:");
int r1=sc.nextInt( );
System.out.print("Enter column for Matrix 1:");
int c1=sc.nextInt( );
System.out.print("Enter row for Matrix 2:");
int r2=sc.nextInt( );
System.out.print("Enter column for Matrix 2:");
int c2=sc.nextInt( );
if (c1!=r2)
System.out.print("Matrix multiplication not possible.");
else
{
int i, j, k, s=0;
int arr[][]= new int[r1][c1];
int brr[][]= new int[r2][c2];
for(i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
{
System.out.print("Enter number for matrix 1:");
arr[i][j]=sc.nextInt( );
}
}
for(i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
{
System.out.print("Enter number for matrix 2:");
brr[i][j]=sc.nextInt( );
}
}
System.out.println("\nMatrix 1:");
for(i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println( );
}
System.out.println("\nMatrix 2:");
for(i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
{
System.out.print(brr[i][j]+"\t");
}
System.out.println( );
}
int krr[][]=new int[r1][c2];
System.out.println("\nProduct");
for(i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
for (k=0; k<c1; k++)
s+= arr[i][k]*brr[k][j];
krr[i][j]=s;
System.out.print(krr[i][j]+"\t");
s=0;
}
System.out.println( );
}
}
} //end of main
} //end of class

Variable Description:

Identifier Data Type Description


r1 int To store row value of first matrix.
c1 int To store column value of first matrix.
r2 int To store row value of second matrix.
c2 int To store column value of second matrix.
arr[][] int array To store values in first matrix.
brr[][] int array To store values in second matrix.
krr[][] int array To store multiplication values in third matrix.
i int To run a loop.
j int To run a loop.
k int To run a loop for multiplication.
s int To store sum for matrix multiplication.
Sample Input/Output:
Input:

Output:

Vous aimerez peut-être aussi