Bird
0
0

How can you use ranges to print numbers from 10 down to 5 in Kotlin?

hard📝 Application Q15 of 15
Kotlin - Loops and Ranges
How can you use ranges to print numbers from 10 down to 5 in Kotlin?
Afor (i in 10..5) { println(i) }
Bfor (i in 10 downTo 5) { println(i) }
Cfor (i in 5..10 step -1) { println(i) }
Dfor (i in 10..5 step 1) { println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Understand descending ranges

    To count down, Kotlin uses 'downTo' instead of '..'.
  2. Step 2: Check each option

    for (i in 10 downTo 5) { println(i) } uses '10 downTo 5' which correctly counts down from 10 to 5.
  3. Final Answer:

    for (i in 10 downTo 5) { println(i) } -> Option B
  4. Quick Check:

    Use 'downTo' for descending ranges [OK]
Quick Trick: Use 'downTo' for counting down in ranges [OK]
Common Mistakes:
MISTAKES
  • Using '..' for descending ranges
  • Trying negative step in ascending range
  • Confusing step direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes