Print Primes Between 1-100
Print all prime numbers between 1 and 100.
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 7 of 14
print-primes-between-1-100.py
1# Print primes from 1 to 1002print("Prime numbers from 1 to 100:")3for num in range(2, 101):4 is_prime = True5 for i in range(2, int(num ** 0.5) + 1):6 if num % i == 0:7 is_prime = False8 break9 if is_prime:10 print(num, end=" ")11print()
Output
Prime numbers from 1 to 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
What's going on
Check each number for primality.
Key Concepts:
Outer loop: numbers 2 to 100
Inner loop: check divisors up to sqrt(num)
If no divisor found, it's 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