Harshad Number Check
Check if number is Harshad (divisible by sum of digits).
IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 13 of 14
harshad-number-check.py
1# Take number2num = int(input("Enter a number: "))34# Calculate sum of digits5temp = abs(num)6digit_sum = 07while temp > 0:8 digit_sum += temp % 109 temp //= 101011# Check Harshad12if digit_sum > 0 and abs(num) % digit_sum == 0:13 print("Harshad number")14else:15 print("Not a Harshad number")
Output
Enter a number: 18 Harshad number
What's going on
Number divisible by sum of its digits.
Key Concepts:
Calculate digit sum
Check if number divisible by sum
Handle edge cases
🔗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