Q:

# Kotlin program to multiply two matrices

belongs to collection: Kotlin Array Programs

Given two matrices, we have to multiply them.

Note: For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.

Example:

```    Input:
matrix 1:
[3, 4, 5, 6]
[7, 8, 9, 8]
[3, 2, 1, 2]

matrix 2:
[3, 4]
[2, 3]
[4, 5]
[6, 7]

Output:
[73, 91]
[121, 153]
[29, 37]```

### Program to multiply two matrices in Kotlin

``````package com.includehelp

import java.util.*

// Main function, Entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val scanner = Scanner(System.`in`)

//Input no of rows and column
print("Enter the number of rows and columns of First matrix : ")
val rows1   = scanner.nextInt()
val column1 = scanner.nextInt()

//Input no of rows and column
print("Enter the number of rows and columns of Second matrix : ")
val rows2   = scanner.nextInt()
val column2 = scanner.nextInt()

//No of Col in first Matrix must be Equal to no of rows in second Matrix
if(column1 != rows2){
println("No of Column in first Matrix must be Equal to no of rows in second Matrix !!")
return
}

//Create First Matrix Array
val matrixA     = Array(rows1) { IntArray(column1) }

//Create Second Matrix Array
val matrixB     = Array(rows2) { IntArray(column2) }

// create Product Matrix Array
val productMatrix   = Array(rows1) { IntArray(column2) }

//Input Matrix
println("Enter the Elements of First Matrix (\$rows1 X \$column1} ): ")
for(i in matrixA.indices){
for(j in matrixA[i].indices){
print("matrixA[\$i][\$j]: ")
matrixA[i][j]=scanner.nextInt()
}
}

//Input Matrix
println("Enter the Elements of Second Matrix (\$rows2 X \$column2} ): ")
for(i in matrixB.indices){
for(j in matrixB[i].indices){
print("matrixB[\$i][\$j]: ")
matrixB[i][j]=scanner.nextInt()
}
}

//print Matrix A
println("Matrix A : ")
for(i in matrixA.indices){
println("\${matrixA[i].contentToString()} ")
}

//print Matrix B
println("Matrix B : ")
for(i in matrixB.indices){
println("\${matrixB[i].contentToString()} ")
}

//Product of Matrices
for(i in productMatrix.indices){
for(j in productMatrix[i].indices){
for(k in matrixB.indices)
productMatrix[i][j]=productMatrix[i][j] + (matrixA[i][k] * matrixB[k][j])
}
}

//Print Product of Matrices
println("Product of the Matrices:")
for(i in productMatrix.indices){
println("\${productMatrix[i].contentToString()} ")
}
}
``````

Output

```Run 1:
Enter the number of rows and columns of First matrix : 3
4
Enter the number of rows and columns of Second matrix : 3
3
No of Column in first Matrix must be Equal to no of rows in second Matrix !!
---------------
Run 2:
Enter the number of rows and columns of First matrix : 3
4
Enter the number of rows and columns of Second matrix : 4
2
Enter the Elements of First Matrix (3 X 4} ):
matrixA[0][0]: 3
matrixA[0][1]: 4
matrixA[0][2]: 5
matrixA[0][3]: 6
matrixA[1][0]: 7
matrixA[1][1]: 8
matrixA[1][2]: 9
matrixA[1][3]: 8
matrixA[2][0]: 3
matrixA[2][1]: 2
matrixA[2][2]: 1
matrixA[2][3]: 2
Enter the Elements of Second Matrix (4 X 2} ):
matrixB[0][0]: 3
matrixB[0][1]: 4
matrixB[1][0]: 2
matrixB[1][1]: 3
matrixB[2][0]: 4
matrixB[2][1]: 5
matrixB[3][0]: 6
matrixB[3][1]: 7
Matrix A :
[3, 4, 5, 6]
[7, 8, 9, 8]
[3, 2, 1, 2]
Matrix B :
[3, 4]
[2, 3]
[4, 5]
[6, 7]
Product of the Matrices:
[73, 91]
[121, 153]
[29, 37]
----
Run 3:
Enter the number of rows and columns of First matrix : 2
3
Enter the number of rows and columns of Second matrix : 3
2
Enter the Elements of First Matrix (2 X 3} ):
matrixA[0][0]: 3
matrixA[0][1]: 4
matrixA[0][2]: 5
matrixA[1][0]: 6
matrixA[1][1]: 3
matrixA[1][2]: 2
Enter the Elements of Second Matrix (3 X 2} ):
matrixB[0][0]: 1
matrixB[0][1]: 2
matrixB[1][0]: 2
matrixB[1][1]: 3
matrixB[2][0]: 2
matrixB[2][1]: 1
Matrix A :
[3, 4, 5]
[6, 3, 2]
Matrix B :
[1, 2]
[2, 3]
[2, 1]
Product of the Matrices:
[21, 23]
[16, 23]```