Prime Number Checker
Determine whether a given number is prime, using a for loop with loop-else.
What is a prime number? a whole number greater than 1 that has no divisors other than 1 and itself.
Approach: try dividing n by every number from 2 up to n-1. If none divide evenly, the loop finishes without a break and confirms the number is prime.
Input: One line: a whole number n.
Output: One line: <n> is a prime number or <n> is not a prime number.
17
17 is a prime number
- 1 <= n <= 100000
Hint 1
Numbers less than 2 are never prime.
Hint 2
Loop i from 2 up to (but not including) n; if n % i == 0 for any i, it's not prime — break immediately.
Hint 3
The loop's else block runs only when the loop finishes without hitting break, which means no divisor was found.
Numbers below 2 are never prime, so handle that first. Otherwise loop i from 2 up to n-1: if any i divides n evenly, set a flag to False and break. The loop-else pattern works just as well — the else block only runs when the loop completes without a break, i.e. no divisor was ever found.