Kotlin Program to search an element in the array


In this program, You will learn how to search an element in the array in Kotlin.


List is: 11 22 33 44 55 66 77 88 99 10

Enter for search: 33 => element found

Example: How to search an element in an array in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int
    var f = 0
    val arr = IntArray(5)
    val sc = Scanner(System.`in`)

    print("Enter 5 elements :")

    for (i in 0 until arr.size) {
        arr[i] = sc.nextInt()
    }

    print("Enter a element for search:")
    n = sc.nextInt()

    for (elements in arr) {
        if (n == elements) {
            f = 1
        }
    }

    if (f == 1) {
        println("Element found:$n")
    } else {
        println("Element not found:$n")
    }
}

Output:

Enter 5 elements :10 20 30 40 50
Enter a element for search:40
Element found:40