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
Inputx = 18
Output9
The sum of digits of x is 9, and 18 is divisible by 9, so the answer is 9.
Example 2
Inputx = 23
Output-1
The sum of digits of x is 5, and 23 is not divisible by 5, so the answer is -1.

Constraints

  • 1 <= x <= 100

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate