Check if Number is Prime
Check if a number is prime.
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 8 of 14
check-if-number-is-prime.py
1# Take number2num = int(input("Enter a number: "))34# Check prime5if num < 2:6 print("Not prime")7else:8 is_prime = True9 for i in range(2, int(num ** 0.5) + 1):10 if num % i == 0:11 is_prime = False12 break1314 if is_prime:15 print("Prime")16 else:17 print("Not prime")
Output
Enter a number: 17 Prime Enter a number: 15 Not prime
What's going on
Check if number has any divisors.
Key Concepts:
Numbers < 2 are not prime
Check divisors from 2 to sqrt(num)
If any divisor found, not prime
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum