Kotlin Program to find the largest digit of a number


In this program, You will learn how to find the largest digit of a number in Kotlin.


12342 => 4

34182 => 8

Example: How to find the largest digit of a number in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int
    var r: Int
    var lar = 0

    var sc = Scanner(System.`in`)

    print("Enter a number:")
    n = sc.nextInt()

    while (n > 0) {
        r = n % 10
        if (lar < r) {
            lar = r
        }
        n /= 10
    }

    println("Largest digit:$lar")
}

Output:

Enter a number:4982
Largest digit:9