0% found this document useful (0 votes)
8 views

Matrix Operations

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Matrix Operations

Uploaded by

riddhichaskar750
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

class MatrixInputOutput {
private Scanner sc = new Scanner(System.in);

public void printMatrix(int[][] matrix) {


for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}

public int[][] getMatrix(int rows, int cols) {


int[][] matrix = new int[rows][cols];
System.out.println("Enter the Elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
}
return matrix;
}
}

class MatrixOperation {
public int[][] multiply(int[][] matrixA, int[][] matrixB, int rowsA, int colsA,
int colsB) {
int[][] result = new int[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}

public int[][] add(int[][] matrixA, int[][] matrixB, int rows, int cols) {
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
return result;
}

public int[][] subtract(int[][] matrixA, int[][] matrixB, int rows, int cols) {
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] - matrixB[i][j];
}
}
return result;
}

public int[][] transpose(int[][] matrix, int rows, int cols) {


int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
}

public class Matrix {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("***************** Matrix Operation
******************");

System.out.print("Enter the Rows of Matrix-A: ");


int rowsA = sc.nextInt();
System.out.print("Enter the Columns of Matrix-A: ");
int colsA = sc.nextInt();
System.out.print("Enter the Rows of Matrix-B: ");
int rowsB = sc.nextInt();
System.out.print("Enter the Columns of Matrix-B: ");
int colsB = sc.nextInt();

if (rowsA != rowsB || colsA != colsB) {


System.out.println("Addition and subtraction require matrices of the
same dimensions.");
}

if (colsA != rowsB) {
System.out.println("Matrix multiplication requires that columns of
Matrix A equal rows of Matrix B.");
}

MatrixInputOutput matrixIO = new MatrixInputOutput();


MatrixOperation matrixOperation = new MatrixOperation();

int[][] matrixA = matrixIO.getMatrix(rowsA, colsA);


int[][] matrixB = matrixIO.getMatrix(rowsB, colsB);

System.out.println("Matrix - A:");
matrixIO.printMatrix(matrixA);

System.out.println("Matrix - B:");
matrixIO.printMatrix(matrixB);

while (true) {
System.out.println("Matrix Operation");
System.out.println("1. Matrix Addition");
System.out.println("2. Matrix Subtraction");
System.out.println("3. Matrix Multiplication");
System.out.println("4. Transpose Matrix A");
System.out.println("5. Transpose Matrix B");
System.out.println("6. Exit");
System.out.print("Enter your Choice: ");
int choice = sc.nextInt();

switch (choice) {
case 1:
System.out.println("Matrix Addition Result:");
int[][] additionResult = matrixOperation.add(matrixA, matrixB,
rowsA, colsA);
matrixIO.printMatrix(additionResult);
break;
case 2:
System.out.println("Matrix Subtraction Result (A - B):");
int[][] subtractionResult = matrixOperation.subtract(matrixA,
matrixB, rowsA, colsA);
matrixIO.printMatrix(subtractionResult);
break;
case 3:
if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible
due to dimension mismatch.");
} else {
System.out.println("Matrix Multiplication Result:");
int[][] multiplicationResult =
matrixOperation.multiply(matrixA, matrixB, rowsA, colsA, colsB);
matrixIO.printMatrix(multiplicationResult);
}
break;
case 4:
System.out.println("Transpose of Matrix A:");
int[][] transposeA = matrixOperation.transpose(matrixA, rowsA,
colsA);
matrixIO.printMatrix(transposeA);
break;
case 5:
System.out.println("Transpose of Matrix B:");
int[][] transposeB = matrixOperation.transpose(matrixB, rowsB,
colsB);
matrixIO.printMatrix(transposeB);
break;
case 6:
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 6.");
break;
}
}
}
}

You might also like