Page 326 - Computer science 868 Class 12
P. 326
Program 9 Write a program to perform a simple matrix multiplication.
Note: For matrix multiplication, the column of the first matrix should be equal to the row of
the second matrix.
1 import java.util.*;
2 class MatixMultiplication
3 {
4 public static void main(String args[])
5 {
6 int row1, col1, row2, col2;
7 Scanner s = new Scanner(System.in);
8 System.out.print("Enter the number of rows in the first matrix: ");
9 row1 = s.nextInt();
10 System.out.print("Enter the number of columns in the first matrix: ");
11 col1 = s.nextInt();
12 System.out.print("Enter the number of rows in the second matrix: ");
13 row2 = s.nextInt();
14 System.out.print("Enter the number of columns in the second matrix: ");
15 col2 = s.nextInt();
16 if (col1 != row2)
17 {
18 System.out.println("Matrix multiplication is not possible");
19 return;
20 }
21
22 int a[][] = new int[row1][col1];
23 int b[][] = new int[row2][col2];
24 int c[][] = new int[row1][col2];
25 System.out.println("\nEnter values for matrix A : ");
26 for (int i = 0; i < row1; i++)
27 {
28 for (int j = 0; j < col1; j++)
29 a[i][j] = s.nextInt();
30 }
31 System.out.println("\nEnter values for matrix B : ");
324324 Touchpad Computer Science-XII

