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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.