Skip to content

Created HarshadNumberTest.java and Added function #3722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 31 additions & 40 deletions src/main/java/com/thealgorithms/maths/HarshadNumber.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/thealgorithms/maths/HarshadNumberTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}