Kotlin Program to find the sum of first and last digit of a number


In this program, You will learn how to find the sum of first and last digit of a number in Kotlin.


1234 = 1 + 4 => 5

Example: How to find the sum of first and last digit of a number in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int
    var r: Int
    var fd: Int
    var ld: Int
    var s: Int

    var rev = 0

    var sc = Scanner(System.`in`)

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

    ld = n % 10

    while (n > 0) {
        r = n % 10
        rev = rev * 10 + r
        n /= 10
    }

    fd = rev % 10
    s = fd + ld
    println("Sum of first and last digits:$s")
}

Output:

Enter a number:3452
Sum of first and last digits:5