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'.
Errors in programs
Sum of individual digits in a certain range
Report a typo
Sample Input 1:
1234Sample Output 1:
10Sample Input 2:
9999Sample Output 2:
36Write 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
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.