Posts

Showing posts from September, 2022

Matrix Multiplication in Java

Write a Java program to perform matrix multiplication. Multiplication of two matrices is possible only if number of columns in matrix A = number of rows in matrix B. The dimensions of the resulting matrix will be: Number of rows of matrix A × Number of columns of matrix B. import java.util.Scanner; class Multiply{     public static void main(String[] args){         Scanner in = new Scanner(System.in);         System.out.print("Rows of matrix 1: ");         int r1 = Integer.parseInt(in.nextLine());         System.out.print("Columns of matrix 1: ");         int c1 = Integer.parseInt(in.nextLine());         System.out.print("Rows of matrix 2: ");         int r2 = Integer.parseInt(in.nextLine());         System.out.print("Columns of matrix 2: ");         int c2 = Integer.parseInt(in.nextLine());         if(c1 != r2){             System.out.println("Can't multiply!");             return;         }         int a[][] = new int[r1][c1];