/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/HarshadNumber.java
51 строка
1 KB
Samuel Facchinello
style: enable `NeedBraces` in checkstyle (#5227)
13 июн 2024, 22:00
Не верифицирован
13 июн 2024, 22:00
87b17e0
Код
Авторство
О чём код?
package com.thealgorithms.maths; // Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number public final class HarshadNumber { private HarshadNumber() { } /** * A function to check if a number is Harshad number or not * * @param n The number to be checked * @return {@code true} if {@code a} is Harshad number, otherwise * {@code false} */ public static boolean isHarshad(long n) { if (n <= 0) { return false; } long t = n; long sumOfDigits = 0; while (t > 0) { sumOfDigits += t % 10; t /= 10; } return n % sumOfDigits == 0; } /** * A function to check if a number is Harshad number or not * * @param s The number in String to be checked * @return {@code true} if {@code a} is Harshad number, otherwise * {@code false} */ public static boolean isHarshad(String s) { final Long n = Long.valueOf(s); if (n <= 0) { return false; } int sumOfDigits = 0; for (char ch : s.toCharArray()) { sumOfDigits += ch - '0'; } return n % sumOfDigits == 0; } }