Package g3001_3100.s3099_harshad_number
Class Solution
-
- All Implemented Interfaces:
public final class Solution3099 - Harshad Number.
Easy
An integer divisible by the sum of its digits is said to be a Harshad number. You are given an integer
x. Return the sum of the digits ofxifxis a Harshad number, otherwise, return-1.Example 1:
Input: x = 18
Output: 9
Explanation:
The sum of digits of
xis9.18is divisible by9. So18is a Harshad number and the answer is9.Example 2:
Input: x = 23
Output: \-1
Explanation:
The sum of digits of
xis5.23is not divisible by5. So23is not a Harshad number and the answer is-1.Constraints:
1 <= x <= 100
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegersumOfTheDigitsOfHarshadNumber(Integer x)-
-
Method Detail
-
sumOfTheDigitsOfHarshadNumber
final Integer sumOfTheDigitsOfHarshadNumber(Integer x)
-
-
-
-