Add HarshadNumberTest (#3722)

This commit is contained in:
Taranjeet Singh Kalsi
2022-11-03 18:29:13 +05:30
committed by GitHub
parent 37db41fd6b
commit 37a1659e18
2 changed files with 53 additions and 39 deletions

View File

@ -1,56 +1,47 @@
// Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.util.Scanner; // Wikipedia for Harshad Number : https://en.wikipedia.org/wiki/Harshad_number
public class HarshadNumber { public class HarshadNumber {
public static void main(String[] args) { /**
Scanner sc = new Scanner(System.in); * A function to check if a number is Harshad number or not
System.out.print("Enter a number : "); *
long a = sc.nextLong(); * @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;
checkHarshadNumber(a); long t = n;
int 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 * 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) { public static boolean isHarshad(String s) {
long b = a; long n = Long.valueOf(s);
int sum = 0; if (n <= 0)
return false;
// this is just for showing the explanation else it's of no use you can ommit it int sumOfDigits = 0;
int[] each = new int[Long.toString(a).length()]; for (char ch : s.toCharArray()) {
sumOfDigits += ch - '0';
int c = 0;
while (b > 0) {
sum += b % 10;
each[c] = (int) (b % 10);
b /= 10;
c++;
} }
if (a % sum == 0) { return n % sumOfDigits == 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");
}
} }
} }

View File

@ -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"));
}
}