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
var isIdentityMatrix: Boolean=true
//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()
if(rows!=column) {
println("Matrix should be Square matrix , Rows and Col size must be Same !!")
return
}
//Create Array
val matrixA = 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()
}
}
//print Matrix A
println("Matrix A : ")
for(i in matrixA.indices){
println("${matrixA[i].contentToString()} ")
}
//Determine Identity Matrix
for(i in matrixA.indices){
for(j in matrixA[i].indices){
if(i==j && matrixA[i][j]!=1){
isIdentityMatrix=false
break
}
else if(i!=j && matrixA[i][j]!=0){
isIdentityMatrix=false
break
}
}
}
if(isIdentityMatrix) println("Identity Matrix !! ") else println("Not a Identity Matrix !!")
}
Output
Run 1:
Enter the number of rows and columns of matrix : 4
3
Matrix should be Square matrix , Rows and Col size must be Same
---
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]: 1
matrixA[0][1]: 0
matrixA[0][2]: 0
matrixA[1][0]: 0
matrixA[1][1]: 1
matrixA[1][2]: 0
matrixA[2][0]: 0
matrixA[2][1]: 0
matrixA[2][2]: 1
Matrix A :
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
Identity Matrix !!
--------
Run 3:
Enter the number of rows and columns of matrix : 2
2
Enter the Elements of First Matrix (2 X 2} ):
matrixA[0][0]: 1
matrixA[0][1]: 5
matrixA[1][0]: 3
matrixA[1][1]: 0
Matrix A :
[1, 5]
[3, 0]
Not a Identity Matrix !!
Program to check Identity Matrix in Kotlin
Output
need an explanation for this answer? contact us directly to get an explanation for this answer