Sum of individual digits in a certain range

Report a typo

Write a Kotlin program that will take an integer as an input. This integer should be between 1 and 9999 (inclusive). Your task is to calculate the sum of the individual digits of this integer. If the input is not within the specified range or if it's not a positive integer, your program should print 'Invalid Input'.

Sample Input 1:

1234

Sample Output 1:

10

Sample Input 2:

9999

Sample Output 2:

36
Write a program in Kotlin
import java.util.*

//Start of Kotlin Program
fun main(args: Array<String>) {

//Create a scan instance for readling line from standard Input
val scan = Scanner(System.`in`)

// Reading an integer as input
var userInput = scan.nextLine().trim()

//TODO:
//You need to Check:
//If the user input is an integer
//If the user input is between 1 and 9999 (inclusive)
//If not, print 'Invalid Input'.
//Otherwise, calculate the sum of the individual digits of this integer.
}
// End of Kotlin Program

Create a free account to access the full topic