Harshad number
Detailed guide and Python implementation for the 'Harshad number' problem.
Problem Statement
Write a function is_harshad(n) that takes a positive integer n and returns True if n is a Harshad (Niven) number, or False otherwise. A Harshad number is a number that is divisible by the sum of its digits. For example, 18 is a Harshad number because 1 + 8 = 9, and 18 is divisible by 9.
- •1 <= n <= 10^9
Examples
n = 18
True
Sum of digits = 1 + 8 = 9. Since 18 % 9 == 0, it is a Harshad number.
n = 21
True
Sum of digits = 2 + 1 = 3. Since 21 % 3 == 0, it is a Harshad number.
n = 14
False
Sum of digits = 1 + 4 = 5. Since 14 % 5 != 0, it is not a Harshad number.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Generate Random Numbers in Python
Learn how to generate random numbers in Python. Compare randrange, randint, and uniform float generation with seeding control.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.