Smallest & Largest Digit
Find smallest and largest digit of a number.
AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 5 of 12
smallest-largest-digit.py
1# Take number2num = int(input("Enter a number: "))34# Find smallest and largest digit5temp = abs(num)6smallest = 97largest = 089while temp > 0:10 digit = temp % 1011 if digit < smallest:12 smallest = digit13 if digit > largest:14 largest = digit15 temp //= 101617print(f"Smallest digit: {smallest}")18print(f"Largest digit: {largest}")
Output
Enter a number: 45231 Smallest digit: 1 Largest digit: 5
What's going on
Track smallest and largest while processing digits.
Key Concepts:
Initialize smallest to 9, largest to 0
Update as you process each digit
Compare and update values
🔗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