904. Check if a Number is a Harshad (Niven) Number
Given a positive integer N, determine whether it is a Harshad number (also known as a Niven number) or not.
A number is said to be a Harshad number if it is divisible by the sum of its digits.
Example 1:
Input: 378
Output: true
Explanation:
- Sum of digits = 3 + 7 + 8 = 18
- 378 is divisible by 18 → 378 % 18 == 0
- So, it is a Harshad number.
Example 2:
Input: 379
Output: false
Explanation:
- Sum of digits = 3 + 7 + 9 = 19
- 379 is not divisible by 19 → 379 % 19 != 0
- So, it is not a Harshad number.
Now Your Turn!
Pick the correct output for the given inputInput: 18
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= N <= 109
- Do not use any inbuilt libraries for digit sum.