You want to print all odd numbers from 1 to 15 using a Kotlin for loop. Which code snippet achieves this?
hard📝 Application Q8 of 15
Kotlin - Loops and Ranges
You want to print all odd numbers from 1 to 15 using a Kotlin for loop. Which code snippet achieves this?
Afor (i in 1..15) { if (i % 2 == 1) println(i) }
BAll of the above
Cfor (i in 1..15 step 1) { if (i % 2 != 0) println(i) }
Dfor (i in 1..15 step 2) { println(i) }
Step-by-Step Solution
Solution:
Step 1: Analyze each option for printing odd numbers
for (i in 1..15 step 2) { println(i) } uses step 2 starting at 1, so prints 1,3,5,...15. for (i in 1..15) { if (i % 2 == 1) println(i) } checks odd numbers with condition inside loop. for (i in 1..15 step 1) { if (i % 2 != 0) println(i) } does the same with step 1.
Step 2: Confirm all options print odd numbers correctly
All options print odd numbers from 1 to 15 correctly.
Final Answer:
All of the above -> Option B
Quick Check:
Multiple ways to print odds = A [OK]
Quick Trick:Use step 2 or condition to print odd numbers [OK]
Common Mistakes:
MISTAKES
Using step 2 starting at even number
Forgetting to check odd condition
Assuming only one correct method
Master "Loops and Ranges" in Kotlin
9 interactive learning modes - each teaches the same concept differently