Prime Check
beginnerUp next
Lists
Store ordered collections of values and learn to add, remove, sort, and loop over them.
Next lessonUp next
Store ordered collections of values and learn to add, remove, sort, and loop over them.
Next lessonOutput will appear here...
Read an integer n and print prime if it is a prime number, or not prime if it isn't.
A prime number is greater than 1 and divisible only by 1 and itself. So 17 is prime, but 18 isn't (it's divisible by 2, 3, 6, and 9), and 1 is not prime.
Tip
Try every possible divisor from 2 up to n - 1. If any of them divides n evenly, you can break out of the loop early. A variable that remembers whether you found a divisor is a handy trick.
No comments yet. Start the discussion.