Knowee
Questions
Features
Study Tools

Write a java program to perform matrix multiplicationinput:m-no. of rowsn-no.of columsmatrix1=[]matrix2=[]output:matrix3=[]

Question

Write a Java program to perform matrix multiplication

Input:
m - number of rows
n - number of columns
matrix1 = []
matrix2 = []

Output:
matrix3 = []

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here’s a Java program that performs matrix multiplication based on the given input structure. The user is prompted to enter the dimensions of the matrices and their elements, and the program outputs the resulting matrix after multiplication.

import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Input dimensions for matrix 1
        System.out.print("Enter the number of rows for matrix 1 (m): ");
        int m = scanner.nextInt();
        System.out.print("Enter the number of columns for matrix 1 (n): ");
        int n = scanner.nextInt();

        // Input dimensions for matrix 2
        System.out.print("Enter the number of rows for matrix 2 (n): ");
        int n2 = n; // same as the columns of matrix 1
        System.out.print("Enter the number of columns for matrix 2 (p): ");
        int p = scanner.nextInt();

        // Ensure matrix multiplication is possible
        if (n2 != m) {
            System.out.println("Matrix multiplication is not possible. Columns of matrix 1 must equal rows of matrix 2.");
            return;
        }

        // Initialize matrices
        int[][] matrix1 = new int[m][n];
        int[][] matrix2 = new int[n][p];
        int[][] matrix3 = new int[m][p];

        // Input matrix 1
        System.out.println("Enter elements of matrix 1:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix1[i][j] = scanner.nextInt();
            }
        }

        // Input matrix 2
        System.out.println("Enter elements of matrix 2:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < p; j++) {
                matrix2[i][j] = scanner.nextInt();
            }
        }

        // Matrix multiplication
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                for (int k = 0; k < n; k++) {
                    matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
                }
            }
        }

        // Output the resulting matrix
        System.out.println("Resulting matrix after multiplication:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                System.out.print(matrix3[i][j] + " ");
            }
            System.out.println();
        }

        scanner.close();
    }
}

Instructions to Run the Program

  1. Copy the code above into a file named MatrixMultiplication.java.
  2. Compile the program using the command: javac MatrixMultiplication.java
  3. Run the program using the command: java MatrixMultiplication
  4. Follow the prompts to enter the dimensions and elements of the matrices.

Functionality

  • The program checks if matrix multiplication is possible by validating the dimensions.
  • It performs the multiplication and outputs the resulting matrix.

This problem has been solved

Similar Questions

Write a python program for multiplication of 2×3 and 3×2 matrix. Input should be taken from user. Put compiled Program and output snapshot here.

Perform the matrix multiplication: [3 1; 4 2] * [2 0; 1 3].a.[5 3; 10 6]b.[3 1; 4 2]c.[7 3; 10 6]d.[7 1; 10 4]

Strassen's matrix multiplication algorithm follows .................................................. technique.

To create a multiplication tableuse the MULTIPLY functionmultiply a row times a column as an arrayuse the MATH librarymultiply a row times a column as range

If X is a matrix with 3 rows and 2 columns, and Y is a matrix with 2 rows and 3 columns, then we can perform the multiplication YX but not XY.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.