Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #199 from iamatifmoin/master
Browse files Browse the repository at this point in the history
Added program for Matrix Multiplication (Helps is issue #1 )
  • Loading branch information
Pradyuman7 authored Oct 2, 2022
2 parents 399561e + 98b476b commit 96771b0
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions MatrixOperations/matrixmultiplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Java program to multiply two square matrices.

import java.io.*;

class matrixmultiplication {

// Function to print Matrix
static void printMatrix(int M[][],
int rowSize,
int colSize) {
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
System.out.print(M[i][j] + " ");

System.out.println();
}
}

// Function to multiply
// two matrices A[][] and B[][]
static void multiplyMatrix(
int row1, int col1, int A[][],
int row2, int col2, int B[][]) {
int i, j, k;

// Print the matrices A and B
System.out.println("\nMatrix A:");
printMatrix(A, row1, col1);
System.out.println("\nMatrix B:");
printMatrix(B, row2, col2);

// Check if multiplication is Possible
if (row2 != col1) {

System.out.println(
"\nMultiplication Not Possible");
return;
}

// Matrix to store the result
// The product matrix will
// be of size row1 x col2
int C[][] = new int[row1][col2];

// Multiply the two matrices
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}

// Print the result
System.out.println("\nResultant Matrix:");
printMatrix(C, row1, col2);
}

// Driver code
public static void main(String[] args) {

int row1 = 4, col1 = 3, row2 = 3, col2 = 4;

int A[][] = { { 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 },
{ 4, 4, 4 } };

int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 } };

multiplyMatrix(row1, col1, A,
row2, col2, B);
}
}

0 comments on commit 96771b0

Please sign in to comment.