# Two Matrix Multiplication

Here given code implementation process.

``````/*
C Program
+ Perform the two matrix multiplication
*/
#include <stdio.h>

#define ROW 3
#define COL 3

//Display the element of given 2d matrix
void show_data(int matrix[][COL]) {

printf("-----------------\n");

for (int i = 0; i < ROW; ++i) {

for (int j = 0; j < COL; ++j) {

printf("%4d", matrix[i][j]);
}

printf("\n");
}

printf("\n");
}
void multiplication(int matrix1[][COL],int matrix2[][COL])
{

//This matrix are store the result of multiplication
int result[ROW][COL];

for (int i = 0; i < ROW; ++i) {

for (int j = 0; j < COL; ++j) {

//Set the initial value of new matrix element
result[i][j] = 0;

for (int k = 0; k < ROW; ++k) {

//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] *matrix2[k][j];
}
}
}
//Display Matrix Elements
printf("  Matrix A\n");
//print element of matrix1
show_data(matrix1);

printf("\n  Matrix B\n");
//print element of matrix1
show_data(matrix2);

printf("\n (Matrix A) X \n (Matrix B)\n");
//Display resultant matrix
show_data(result);

}
int main() {
//Define matrix 1
int matrix1[ROW][COL] = {
{
1, 2, 3
},

{
6, 1, 2
},
{
5, 4, 3
}
};

//Define matrix 2
int matrix2[ROW][COL] = {
{
3, 1, 3
},
{
1, 1, 2
},
{
2, 2, 3
}
};

multiplication(matrix1,matrix2);

return 0;
}```
```

#### Output

``````  Matrix A
-----------------
1   2   3
6   1   2
5   4   3

Matrix B
-----------------
3   1   3
1   1   2
2   2   3

(Matrix A) X
(Matrix B)
-----------------
11   9  16
23  11  26
25  15  32
``````
``````#include<iostream>
#define ROW 3
#define COL 3
using namespace std;
/*
C++ Program
Perform the two matrix multiplication
*/

class MyMatrix {
public:

//Display the element of given 2d matrix
void show_data(int matrix[ROW][COL]) {
cout << "-----------------\n";

for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
cout << " " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
void multiplication(int matrix1[ROW][COL], int matrix2[ROW][COL]) {
//This matrix are store the result of multiplication
int result[ROW][COL];
for (int i = 0; i < ROW; ++i) {
for (int j = 0; j < COL; ++j) {
//Set the initial value of new matrix element
result[i][j] = 0;
for (int k = 0; k < ROW; ++k) {
//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] *matrix2[k][j];
}
}
}
cout << " Matrix A\n";
//print element of matrix1
this->show_data(matrix1);
cout << "\n Matrix B\n";
//print element of matrix1
this->show_data(matrix2);
cout << "\n (Matrix A) X \n (Matrix B)\n";
//Display resultant matrix
this->show_data(result);
}
};
int main() {
MyMatrix obj ;
//Define matrix 1
int matrix1[ROW][COL] = {
{
1,
2,
3
},
{
6,
1,
2
},
{
5,
4,
3
}
};
int matrix2[ROW][COL] = {
{
3,
1,
3
},
{
1,
1,
2
},
{
2,
2,
3
}
};
obj.multiplication(matrix1, matrix2);
return 0;
}```
```

#### Output

`````` Matrix A
-----------------
1 2 3
6 1 2
5 4 3

Matrix B
-----------------
3 1 3
1 1 2
2 2 3

(Matrix A) X
(Matrix B)
-----------------
11 9 16
23 11 26
25 15 32
``````
``````/*
Java Program
Perform the two matrix multiplication
*/
public class MyMatrix {

//Display the element of given 2d matrix
public void show_data(int [][]matrix) {

System.out.print("-----------------\n");

//Assume  N x N Matrix size
int row = matrix.length;

int col = matrix[0].length;

for (int i = 0; i < row; ++i) {

for (int j = 0; j < col; ++j) {

System.out.print("  "+ matrix[i][j]);
}

System.out.print("\n");
}

System.out.print("\n");
}
public void multiplication(int [][]matrix1,int [][]matrix2)
{
//Assume both matrix are equal size
//Assume  N x N Matrix size
int row = matrix1.length;

int col = matrix1[0].length;

//This matrix are store the result of multiplication
int [][]result = new int[row][col];

for (int i = 0; i < row; ++i) {

for (int j = 0; j < col; ++j) {

//Set the initial value of new matrix element
result[i][j] = 0;

for (int k = 0; k < row; ++k) {

//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.print("  Matrix A\n");
//print element of matrix1
show_data(matrix1);

System.out.print("\n  Matrix B\n");
//print element of matrix1
show_data(matrix2);

System.out.print("\n (Matrix A) X \n (Matrix B)\n");
//Display resultant matrix
show_data(result);

}

public static void main(String[] args) {

MyMatrix obj = new MyMatrix();
//Define matrix 1
int [][]matrix1 = {
{
1, 2, 3
},

{
6, 1, 2
},
{
5, 4, 3
}
};

//Define matrix 2
int [][]matrix2 = {
{
3, 1, 3
},
{
1, 1, 2
},
{
2, 2, 3
}
};

obj.multiplication(matrix1,matrix2);

}
}```
```

#### Output

`````` Matrix A
-----------------
1  2  3
6  1  2
5  4  3

Matrix B
-----------------
3  1  3
1  1  2
2  2  3

(Matrix A) X
(Matrix B)
-----------------
11  9  16
23  11  26
25  15  32
``````
``````/*
C# Program
Perform the two matrix multiplication
*/
using System;
public class MyMatrix {

//Display the element of given 2d matrix
public void show_data(int[,] matrix) {

Console.Write("-----------------\n");

//Assume  N x N Matrix size
int row = matrix.GetLength(0);

int col = matrix.GetLength(1);

for (int i = 0; i < row; ++i) {

for (int j = 0; j < col; ++j) {

Console.Write("  " + matrix[i,j]);
}

Console.Write("\n");
}

Console.Write("\n");
}
public void multiplication(int[,] matrix1, int[,] matrix2) {
//Assume both matrix are equal size
//Assume  N x N Matrix size
int row = matrix1.GetLength(0);

int col = matrix1.GetLength(1);

//This matrix are store the result of multiplication
int[,] result = new int[row,col];

for (int i = 0; i < row; ++i) {

for (int j = 0; j < col; ++j) {

//Set the initial value of new matrix element
result[i,j] = 0;

for (int k = 0; k < row; ++k) {

//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i,j] += matrix1[i,k] * matrix2[k,j];
}
}
}
Console.Write("  Matrix A\n");
//print element of matrix1
show_data(matrix1);

Console.Write("\n  Matrix B\n");
//print element of matrix1
show_data(matrix2);

Console.Write("\n (Matrix A) X \n (Matrix B)\n");
//Display resultant matrix
show_data(result);

}

public static void Main(String[] args) {

MyMatrix obj = new MyMatrix();
//Define matrix 1
int[,] matrix1 = {
{
1,
2,
3
},

{
6,
1,
2
},
{
5,
4,
3
}
};

//Define matrix 2
int[,] matrix2 = {
{
3,
1,
3
},
{
1,
1,
2
},
{
2,
2,
3
}
};

obj.multiplication(matrix1, matrix2);

}
}```
```

#### Output

``````  Matrix A
-----------------
1  2  3
6  1  2
5  4  3

Matrix B
-----------------
3  1  3
1  1  2
2  2  3

(Matrix A) X
(Matrix B)
-----------------
11  9  16
23  11  26
25  15  32
``````
``````<?php
/*
Php Program
Perform the two matrix multiplication
*/
class MyMatrix {
//Display the element of given 2d matrix

public 	function show_data(\$matrix) {
echo("-----------------\n");
//Assume  N x N Matrix size
\$row = count(\$matrix);
\$col = count(\$matrix[0]);
for (\$i = 0; \$i < \$row; ++\$i) {
for (\$j = 0; \$j < \$col; ++\$j) {
echo(" ". \$matrix[\$i][\$j]);
}
echo("\n");
}
echo("\n");
}
public 	function multiplication(\$matrix1, \$matrix2) {
//Assume both matrix are equal size
//Assume  N x N Matrix size
\$row = count(\$matrix1);
\$col = count(\$matrix1[0]);
//This matrix are store the result of multiplication
\$result = array_fill(0, \$row,array_fill(0,\$row,0));
for (\$i = 0; \$i < \$row; ++\$i) {
for (\$j = 0; \$j < \$col; ++\$j) {
//Set the initial value of new matrix element
\$result[\$i][\$j] = 0;
for (\$k = 0; \$k < \$row; ++\$k) {
//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
\$result[\$i][\$j] += \$matrix1[\$i][\$k] *\$matrix2[\$k][\$j];
}
}
}
echo(" Matrix A\n");
//print element of matrix1
\$this->show_data(\$matrix1);
echo("\n Matrix B\n");
//print element of matrix1
\$this->show_data(\$matrix2);
echo("\n (Matrix A) X \n (Matrix B)\n");
//Display resultant matrix
\$this->show_data(\$result);
}
};

function main() {
\$obj = new MyMatrix();
//Define matrix 1
\$matrix1 = array(array(1, 2, 3), array(6, 1, 2), array(5, 4, 3));
//Define matrix 2
\$matrix2 = array(array(3, 1, 3), array(1, 1, 2), array(2, 2, 3));
\$obj->multiplication(\$matrix1, \$matrix2);
}
main();```
```

#### Output

`````` Matrix A
-----------------
1 2 3
6 1 2
5 4 3

Matrix B
-----------------
3 1 3
1 1 2
2 2 3

(Matrix A) X
(Matrix B)
-----------------
11 9 16
23 11 26
25 15 32
``````
``````/*
Node Js Program
Perform two matrix multiplication
*/
class MyMatrix {
//Display the element of given 2d matrix
show_data(matrix) {
process.stdout.write("-----------------\n");
//Assume  N x N Matrix size
var row = matrix.length;
var col = matrix[0].length;
for (var i = 0; i < row; ++i) {
for (var j = 0; j < col; ++j) {
process.stdout.write(" " + matrix[i][j]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
multiplication(matrix1, matrix2) {
//Assume both matrix are equal size
//Assume  N x N Matrix size
var row = matrix1.length;
var col = matrix1[0].length;
//This matrix are store the result of multiplication
//Set the initial value of new matrix element
var result = Array(row).fill(0).map(() => new Array(col).fill(0));

for (var i = 0; i < row; ++i) {
result[i][j] = 0;
for (var j = 0; j < col; ++j) {

for (var k = 0; k < row; ++k) {
//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] *matrix2[k][j];
}
}
}
process.stdout.write(" Matrix A\n");
//print element of matrix1
this.show_data(matrix1);
process.stdout.write("\n Matrix B\n");
//print element of matrix1
this.show_data(matrix2);
process.stdout.write("\n (Matrix A) X \n (Matrix B)\n");
//Display resultant matrix
this.show_data(result);
}
}

function main(args) {
var obj = new MyMatrix();
//Define matrix 1
var matrix1 = [
[1, 2, 3],
[6, 1, 2],
[5, 4, 3]
];
//Define matrix 2
var matrix2 = [
[3, 1, 3],
[1, 1, 2],
[2, 2, 3]
];
obj.multiplication(matrix1, matrix2)
}
main();```
```

#### Output

`````` Matrix A
-----------------
1 2 3
6 1 2
5 4 3

Matrix B
-----------------
3 1 3
1 1 2
2 2 3

(Matrix A) X
(Matrix B)
-----------------
11 9 16
23 11 26
25 15 32
``````
``````# Python 3 Program
# Perform two matrix multiplication
class MyMatrix :
# Display the element of given 2d matrix
def show_data(self, matrix) :
print("-----------------")
row = len(matrix)
col = len(matrix[0])
i = 0
while (i < row) :
j = 0
while (j < col) :
print(" ", matrix[i][j],end="")
j += 1

print(end="\n")
i += 1

print(end="\n")

def multiplication(self, matrix1, matrix2) :
# Assume both matrix are equal size
# Assume  N x N Matrix size
row = len(matrix1)
col = len(matrix1[0])
# This matrix are store the result of multiplication
result = [[0 for i in range(col)] for j in range(row)]
i = 0
while (i < row) :
j = 0
while (j < col) :
# Set the initial value of new matrix element
result[i][j] = 0
k = 0
while (k < row) :
# Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] * matrix2[k][j]
k += 1

j += 1

i += 1

print(" Matrix A")
self.show_data(matrix1)
print("\n Matrix B")
self.show_data(matrix2)
print("\n (Matrix A) X \n (Matrix B)")
self.show_data(result)

def main() :
obj = MyMatrix()
matrix1 = [
[1, 2, 3],
[6, 1, 2],
[5, 4, 3]
]
matrix2 = [
[3, 1, 3],
[1, 1, 2],
[2, 2, 3]
]
obj.multiplication(matrix1, matrix2)

if __name__ == "__main__":
main()```
```

#### Output

`````` Matrix A
-----------------
1  2  3
6  1  2
5  4  3

Matrix B
-----------------
3  1  3
1  1  2
2  2  3

(Matrix A) X
(Matrix B)
-----------------
11  9  16
23  11  26
25  15  32
``````
``````# Ruby Program
# Perform two matrix multiplication
class MyMatrix
# Display the element of given 2d matrix
def show_data(matrix)
print("-----------------\n")
row = matrix.length
col = matrix[0].length
i = 0
while (i < row)
j = 0
while (j < col)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
def multiplication(matrix1, matrix2)
# Assume both matrix are equal size
# Assume  N x N Matrix size
row = matrix1.length
col = matrix1[0].length
# This matrix are store the result of multiplication
result = Array.new(row){ Array.new(col, 0) }
i = 0
while (i < row)
j = 0
while (j < col)
# Set the initial value of new matrix element
result[i][j] = 0
k = 0
while (k < row)
# Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] * matrix2[k][j]
k += 1
end
j += 1
end
i += 1
end
print(" Matrix A\n")
self.show_data(matrix1)
print("\n Matrix B\n")
self.show_data(matrix2)
print("\n (Matrix A) X \n (Matrix B)\n")
self.show_data(result)
end
end
def main()
obj = MyMatrix.new()
matrix1 = [
[1, 2, 3],
[6, 1, 2],
[5, 4, 3]
]
matrix2 = [
[3, 1, 3],
[1, 1, 2],
[2, 2, 3]
]
obj.multiplication(matrix1, matrix2)
end
main()```
```

#### Output

`````` Matrix A
-----------------
1 2 3
6 1 2
5 4 3

Matrix B
-----------------
3 1 3
1 1 2
2 2 3

(Matrix A) X
(Matrix B)
-----------------
11 9 16
23 11 26
25 15 32

``````
``````/*
Scala Program
Perform two matrix multiplication
*/
class MyMatrix {
//Display the element of given 2d matrix
def show_data(matrix: Array[Array[Int]]): Unit = {
print("-----------------\n");
var row: Int = matrix.length;
var col: Int = matrix(0).length;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
def multiplication(matrix1: Array[Array[Int]], matrix2: Array[Array[Int]]): Unit = {
//Assume both matrix are equal size
//Assume  N x N Matrix size
var row: Int = matrix1.length;
var col: Int = matrix1(0).length;
//This matrix are store the result of multiplication
var result: Array[Array[Int]] = Array.fill[Int](row,col)(0);
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
//Set the initial value of new matrix element
result(i)(j) = 0;
var k: Int = 0;
while (k < row) {
//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result(i)(j) += matrix1(i)(k) * matrix2(k)(j);
k += 1;
}
j += 1;
}
i += 1;
}
print(" Matrix A\n");
this.show_data(matrix1);
print("\n Matrix B\n");
this.show_data(matrix2);
print("\n (Matrix A) X \n (Matrix B)\n");
this.show_data(result);
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
var matrix1: Array[Array[Int]] = Array(
Array(1, 2, 3),
Array(6, 1, 2),
Array(5, 4, 3)
);
var matrix2: Array[Array[Int]] = Array(
Array(3, 1, 3),
Array(1, 1, 2),
Array(2, 2, 3)
);
obj.multiplication(matrix1, matrix2);
}
}```
```

#### Output

`````` Matrix A
-----------------
1 2 3
6 1 2
5 4 3

Matrix B
-----------------
3 1 3
1 1 2
2 2 3

(Matrix A) X
(Matrix B)
-----------------
11 9 16
23 11 26
25 15 32
``````
``````/*
Swift 4 Program
Perform two matrix multiplication
*/
class MyMatrix {
//Display the element of given 2d matrix
func show_data(_ matrix: [
[Int]
]) {
print("-----------------");
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
print(" ", matrix[i][j],terminator:"");
j += 1;
}
print(terminator:"\n");
i += 1;
}
print(terminator:"\n");
}
func multiplication(_ matrix1: [
[Int]
], _ matrix2: [
[Int]
]) {
//Assume both matrix are equal size
//Assume  N x N Matrix size
let row: Int = matrix1.count;
let col: Int = matrix1[0].count;
//This matrix are store the result of multiplication
var result: [[Int]] = Array(repeating : Array( repeating:0 , count:col) ,  count:row);
var i: Int = 0;
while (i < row) {
var j: Int = 0;
while (j < col) {
//Set the initial value of new matrix element
result[i][j] = 0;
var k: Int = 0;
while (k < row) {
//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
result[i][j] += matrix1[i][k] * matrix2[k][j];
k += 1;
}
j += 1;
}
i += 1;
}
print(" Matrix A");
self.show_data(matrix1);
print("\n Matrix B");
self.show_data(matrix2);
print("\n (Matrix A) X \n (Matrix B)");
self.show_data(result);
}
}
func main() {
let obj: MyMatrix = MyMatrix();
let matrix1: [
[Int]
] = [
[1, 2, 3],
[6, 1, 2],
[5, 4, 3]
];
let matrix2: [
[Int]
] = [
[3, 1, 3],
[1, 1, 2],
[2, 2, 3]
];
obj.multiplication(matrix1, matrix2);
}
main();```
```

#### Output

`````` Matrix A
-----------------
1  2  3
6  1  2
5  4  3

Matrix B
-----------------
3  1  3
1  1  2
2  2  3

(Matrix A) X
(Matrix B)
-----------------
11  9  16
23  11  26
25  15  32
``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.