-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHarshadNumberCheck.java
More file actions
28 lines (25 loc) · 867 Bytes
/
HarshadNumberCheck.java
File metadata and controls
28 lines (25 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 42. Write a program to check if a given number is a Harshad number.
import java.util.Scanner;
public class HarshadNumberCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isHarshad = isHarshadNumber(num);
if (isHarshad) {
System.out.println(num + " is a Harshad number.");
} else {
System.out.println(num + " is not a Harshad number.");
}
scanner.close();
}
public static boolean isHarshadNumber(int num) {
int sumOfDigits = 0;
int originalNum = num;
while (num > 0) {
sumOfDigits += num % 10;
num /= 10;
}
return originalNum % sumOfDigits == 0;
}
}