Reputation: 2641
How do I write code to return a range of 10 numbers from a given number.
i.e if I am given 5, code should return 0..9
358 should return 350..359
33 should return 30..39 etc
Upvotes: 1
Views: 891
Reputation: 11
You can use the following code:
fun answer(givenNum: Int) : IntRange {
val startOfRange = givenNum - (givenNum % 10)
return startOfRange until (startOfRange + 10)
}
fun main() {
val givenNum = 33
println(answer(givenNum)) // 30..39
}
Upvotes: 1
Reputation: 7618
If the given number is integer type, you can simply write
val x = 358
(x / 10 * 10)..(x / 10 * 10 + 9)
Upvotes: 5
Reputation: 2822
Do you mean something like this?
fun range10(contained: Int): IntRange {
val start = contained - contained % 10
val end = start + 9
return start..end
}
Upvotes: 7