Home »
Kotlin »
Kotlin Programs »
Kotlin Array Programs
Kotlin program to add two matrices
Kotlin | Two matrices addition: Here, we are going to learn how to add two given matrices using multi-dimensional array in Kotlin programming language?
Submitted by IncludeHelp, on May 06, 2020
Kotlin - Add two matrices
Given two matrices, we have to add them.
Example:
Input:
matrix 1:
[2, 3]
[4, 5]
[7, 1]
matrix 2:
[4, 6]
[9, 0]
[7, 6]
Output:
[6, 9]
[13, 5]
[14, 7]
Program to add two matrices in Kotlin
package com.includehelp
import java.util.*
// Main function, Entry Point of Program
fun main(args: Array<String>) {
//variable of rows and col
val rows: Int
val column: Int
//Input Stream
val scanner = Scanner(System.`in`)
//Input no of rows and column
print("Enter the number of rows and columns of matrix : ")
rows = scanner.nextInt()
column = scanner.nextInt()
//Create Array
val matrixA = Array(rows) { IntArray(column) }
val matrixB = Array(rows) { IntArray(column) }
val matrixSum = Array(rows) { IntArray(column) }
//Input Matrix
println("Enter the Elements of First Matrix ($rows X $column} ): ")
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 ($rows X $column} ): ")
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()} ")
}
//Perform Addition
for(i in matrixSum.indices){
for(j in matrixSum[i].indices){
matrixSum[i][j]=matrixA[i][j]+matrixB[i][j]
}
}
//Print Sum of Matrices
println("Sum of the Matrices:")
for(i in matrixSum.indices){
println("${matrixSum[i].contentToString()} ")
}
}
Output
Run 1:
Enter the number of rows and columns of matrix : 3
2
Enter the Elements of First Matrix (3 X 2} ):
matrixA[0][0]: 2
matrixA[0][1]: 3
matrixA[1][0]: 4
matrixA[1][1]: 5
matrixA[2][0]: 7
matrixA[2][1]: 1
Enter the Elements of Second Matrix (3 X 2} ):
matrixB[0][0]: 4
matrixB[0][1]: 6
matrixB[1][0]: 9
matrixB[1][1]: 0
matrixB[2][0]: 7
matrixB[2][1]: 6
Matrix A :
[2, 3]
[4, 5]
[7, 1]
Matrix B :
[4, 6]
[9, 0]
[7, 6]
Sum of the Matrices:
[6, 9]
[13, 5]
[14, 7]
---------------
Run 2:
Enter the number of rows and columns of matrix : 3
3
Enter the Elements of First Matrix (3 X 3} ):
matrixA[0][0]: 2
matrixA[0][1]: 3
matrixA[0][2]: 4
matrixA[1][0]: 5
matrixA[1][1]: 6
matrixA[1][2]: 7
matrixA[2][0]: 0
matrixA[2][1]: 9
matrixA[2][2]: -7
Enter the Elements of Second Matrix (3 X 3} ):
matrixB[0][0]: 4
matrixB[0][1]: 0
matrixB[0][2]: -6
matrixB[1][0]: -3
matrixB[1][1]: 2
matrixB[1][2]: 3
matrixB[2][0]: 4
matrixB[2][1]: 5
matrixB[2][2]: 6
Matrix A :
[2, 3, 4]
[5, 6, 7]
[0, 9, -7]
Matrix B :
[4, 0, -6]
[-3, 2, 3]
[4, 5, 6]
Sum of the Matrices:
[6, 3, -2]
[2, 8, 10]
[4, 14, -1]