Home »
Kotlin »
Kotlin Programs »
Kotlin Array Programs
Kotlin program to find frequencies of even and odd numbers in a matrix
Here, we are going to learn how to find frequencies of even and odd numbers in a matrix in Kotlin programming language?
Submitted by IncludeHelp, on May 06, 2020
Kotlin - Find frequencies of even and odd numbers in a matrix
Given a matrix, we have to find frequencies of even and odd numbers.
Example:
Input:
matrix:
[4, 5]
[6, 0]
[9, 2]
Output:
Even Elements Frequency : 4
Odd Elements Frequency : 2
Program to find frequencies of even and odd numbers in a matrix 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
var oddCount = 0
var evenCount = 0
//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) }
//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()
//count Odd and Even elements Frequency
if(matrixA[i][j]%2==0) evenCount++ else oddCount++
}
}
//print Matrix A
println("Matrix A : ")
for(i in matrixA.indices){
println("${matrixA[i].contentToString()} ")
}
//Print Frequency
println("Even Elements Frequency : $evenCount")
println("Odd Elements Frequency : $oddCount")
}
Output
Run 1:
Enter the number of rows and columns of matrix : 2
4
Enter the Elements of First Matrix (2 X 4} ):
matrixA[0][0]: 3
matrixA[0][1]: 4
matrixA[0][2]: 5
matrixA[0][3]: 7
matrixA[1][0]: 0
matrixA[1][1]: 9
matrixA[1][2]: 1
matrixA[1][3]: -4
Matrix A :
[3, 4, 5, 7]
[0, 9, 1, -4]
Even Elements Frequency : 3
Odd Elements Frequency : 5
-----------
Run 2:
Enter the number of rows and columns of matrix : 3
2
Enter the Elements of First Matrix (3 X 2} ):
matrixA[0][0]: 4
matrixA[0][1]: 5
matrixA[1][0]: 6
matrixA[1][1]: 0
matrixA[2][0]: 9
matrixA[2][1]: 2
Matrix A :
[4, 5]
[6, 0]
[9, 2]
Even Elements Frequency : 4
Odd Elements Frequency : 2