Repository: TheAlgorithms/Java
Pull request #3722 (2022-10-28 13:05:47)

Created HarshadNumberTest.java and Added function
<!-- For completed items, change [ ] to [x] -->
 Created HarshadNumberTest.java
  Added another function, removed main(), and refactored code in HarshadNumber.java
- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/HarshadNumber.java b/src/main/java/com/thealgorithms/maths/HarshadNumber.java
index 9b24209b..b30ada48 100644
--- a/src/main/java/com/thealgorithms/maths/HarshadNumber.java
+++ b/src/main/java/com/thealgorithms/maths/HarshadNumber.java
@@ -1,56 +1,47 @@
-// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
 package com.thealgorithms.maths;
 
-import java.util.Scanner;
+// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
 
 public class HarshadNumber {
 
-    public static void main(String[] args) {
-        Scanner sc = new Scanner(System.in);
-        System.out.print("Enter a number : ");
-        long a = sc.nextLong();
+    /**
+     * 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;
+        int sumOfDigits = 0;
+        while (t > 0) {
+            sumOfDigits += t % 10;
+            t /= 10;
+        }
 
-        checkHarshadNumber(a);
+        return n % sumOfDigits == 0;
     }
 
     /**
      * A function to check if a number is Harshad number or not
      *
-     * @param a The number which should be checked
+     * @param s The number in String to be checked
+     * @return {@code true} if {@code a} is Harshad number, otherwise
+     *         {@code false}
      */
-    public static void checkHarshadNumber(long a) {
-        long b = a;
-        int sum = 0;
-
-        // this is just for showing the explanation else it's of no use you can ommit it
-        int[] each = new int[Long.toString(a).length()];
-
-        int c = 0;
-
-        while (b > 0) {
-            sum += b % 10;
-            each[c] = (int) (b % 10);
-            b /= 10;
-            c++;
+    public static boolean isHarshad(String s) {
+        long n = Long.valueOf(s);
+        if (n <= 0)
+            return false;
+
+        int sumOfDigits = 0;
+        for (char ch : s.toCharArray()) {
+            sumOfDigits += ch - '0';
         }
 
-        if (a % sum == 0) {
-            System.out.println(a + " is a Harshad Number");
-
-            // For you better explanation how is that a Harshad Number
-            System.out.println("\nExplaination :");
-
-            for (int i = each.length - 1; i >= 0; i--) {
-                System.out.print(each[i] + " ");
-                if (i != 0) {
-                    System.out.print("+ ");
-                }
-            }
-
-            System.out.println("= " + sum);
-            System.out.println(sum + " × " + (a / sum) + " = " + a);
-        } else {
-            System.out.println(a + " is not a Harshad Number");
-        }
+        return n % sumOfDigits == 0;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java b/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java
new file mode 100644
index 00000000..f9176961
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java
@@ -0,0 +1,23 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+public class HarshadNumberTest {
+
+    @Test
+    public void harshadNumber() {
+        
+        assertTrue(HarshadNumber.isHarshad(18));
+        assertFalse(HarshadNumber.isHarshad(-18));
+        assertFalse(HarshadNumber.isHarshad(19));
+        assertTrue(HarshadNumber.isHarshad(999999999));
+        assertFalse(HarshadNumber.isHarshad(0));
+
+        assertTrue(HarshadNumber.isHarshad("18"));
+        assertFalse(HarshadNumber.isHarshad("-18"));
+        assertFalse(HarshadNumber.isHarshad("19"));
+        assertTrue(HarshadNumber.isHarshad("999999999"));
+        assertTrue(HarshadNumber.isHarshad("99999999999100"));
+    }
+}