Skip to content

Commit 37a1659

Browse files
Add HarshadNumberTest (TheAlgorithms#3722)
1 parent 37db41f commit 37a1659

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed
Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
1-
// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
21
package com.thealgorithms.maths;
32

4-
import java.util.Scanner;
3+
// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
54

65
public class HarshadNumber {
76

8-
public static void main(String[] args) {
9-
Scanner sc = new Scanner(System.in);
10-
System.out.print("Enter a number : ");
11-
long a = sc.nextLong();
7+
/**
8+
* A function to check if a number is Harshad number or not
9+
*
10+
* @param n The number to be checked
11+
* @return {@code true} if {@code a} is Harshad number, otherwise
12+
* {@code false}
13+
*/
14+
public static boolean isHarshad(long n) {
15+
if (n <= 0)
16+
return false;
17+
18+
long t = n;
19+
int sumOfDigits = 0;
20+
while (t > 0) {
21+
sumOfDigits += t % 10;
22+
t /= 10;
23+
}
1224

13-
checkHarshadNumber(a);
25+
return n % sumOfDigits == 0;
1426
}
1527

1628
/**
1729
* A function to check if a number is Harshad number or not
1830
*
19-
* @param a The number which should be checked
31+
* @param s The number in String to be checked
32+
* @return {@code true} if {@code a} is Harshad number, otherwise
33+
* {@code false}
2034
*/
21-
public static void checkHarshadNumber(long a) {
22-
long b = a;
23-
int sum = 0;
24-
25-
// this is just for showing the explanation else it's of no use you can ommit it
26-
int[] each = new int[Long.toString(a).length()];
27-
28-
int c = 0;
29-
30-
while (b > 0) {
31-
sum += b % 10;
32-
each[c] = (int) (b % 10);
33-
b /= 10;
34-
c++;
35+
public static boolean isHarshad(String s) {
36+
long n = Long.valueOf(s);
37+
if (n <= 0)
38+
return false;
39+
40+
int sumOfDigits = 0;
41+
for (char ch : s.toCharArray()) {
42+
sumOfDigits += ch - '0';
3543
}
3644

37-
if (a % sum == 0) {
38-
System.out.println(a + " is a Harshad Number");
39-
40-
// For you better explanation how is that a Harshad Number
41-
System.out.println("\nExplaination :");
42-
43-
for (int i = each.length - 1; i >= 0; i--) {
44-
System.out.print(each[i] + " ");
45-
if (i != 0) {
46-
System.out.print("+ ");
47-
}
48-
}
49-
50-
System.out.println("= " + sum);
51-
System.out.println(sum + " × " + (a / sum) + " = " + a);
52-
} else {
53-
System.out.println(a + " is not a Harshad Number");
54-
}
45+
return n % sumOfDigits == 0;
5546
}
5647
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class HarshadNumberTest {
7+
8+
@Test
9+
public void harshadNumber() {
10+
11+
assertTrue(HarshadNumber.isHarshad(18));
12+
assertFalse(HarshadNumber.isHarshad(-18));
13+
assertFalse(HarshadNumber.isHarshad(19));
14+
assertTrue(HarshadNumber.isHarshad(999999999));
15+
assertFalse(HarshadNumber.isHarshad(0));
16+
17+
assertTrue(HarshadNumber.isHarshad("18"));
18+
assertFalse(HarshadNumber.isHarshad("-18"));
19+
assertFalse(HarshadNumber.isHarshad("19"));
20+
assertTrue(HarshadNumber.isHarshad("999999999"));
21+
assertTrue(HarshadNumber.isHarshad("99999999999100"));
22+
}
23+
}

0 commit comments

Comments
 (0)