JuniorMath
Harshad Number
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 of x if x is a Harshad number; otherwise, return -1.
Example 1
Input
x = 18Output
9The sum of digits of
x is 9, and 18 is divisible by 9, so the answer is 9.Example 2
Input
x = 23Output
-1The sum of digits of
x is 5, and 23 is not divisible by 5, so the answer is -1.Constraints
1 <= x <= 100