forked from anish-mishr/specialNumbers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHarshadNumber.java
More file actions
34 lines (30 loc) · 1.1 KB
/
Copy pathHarshadNumber.java
File metadata and controls
34 lines (30 loc) · 1.1 KB
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
29
30
31
32
33
34
package specialNumbers;
/*
* @author Anish
*
*/
public class HarshadNumber {
/* Harshad Number is number which is divisible by the sum of its digits.
156 is divisible by the sum (12) of its digits (1, 5, 6 ).
*/
public static void harshadNumber(int num) { //Static Method
int temp, sum=0, rem;
temp = num;
while(temp>0) {
rem = temp % 10; // gets remainder temporary divided by 10
sum = sum + rem; // gets sum
temp = temp /10; // updates temporary with temporary divided by 10
}
if(num%sum==0) // Checks num divisible by sum of its digits is ZERO (0)
System.out.println(">> "+ num +" is Harshad Number"); //IF YES prints Harshad Number
else
System.out.println(">> "+ num +" is NOT Harshad Number"); //IF NO prints NOT Harshad Number
}
// MAIN METHOD
public static void main(String[] args) {
// Input is another public class having getNumber STATIC Method and a return type of INTEGER
System.out.println(" Find a Number is Harshad Number or NOT");
int num = Input.getNumber(); // Return NUM
harshadNumber(num); // Static method to check the Harshad Number
}
}