g3001_3100.s3099_harshad_number.readme.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
3099\. 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_ of `x` if `x` is a **Harshad** number, otherwise, return `-1`_._
**Example 1:**
**Input:** x = 18
**Output:** 9
**Explanation:**
The sum of digits of `x` is `9`. `18` is divisible by `9`. So `18` is a Harshad number and the answer is `9`.
**Example 2:**
**Input:** x = 23
**Output:** \-1
**Explanation:**
The sum of digits of `x` is `5`. `23` is not divisible by `5`. So `23` is not a Harshad number and the answer is `-1`.
**Constraints:**
* `1 <= x <= 100`