Java Program To Check For Harshad Number
Chapter:
Math Class
Last Updated:
28-05-2016 12:52:55 UTC
Program:
/* ............... START ............... */
import java.util.*;
public class JavaHarshadNumber {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = scanner.nextInt();
int c = n, d, sum = 0;
while (c > 0) {
d = c % 10;
sum = sum + d;
c = c / 10;
}
if (n % sum == 0)
System.out.println(n + " is a Harshad Number.");
else
System.out.println(n + " is not a Harshad Number.");
}
}
/* ............... END ............... */
Output
Enter a number : 195
195 is a Harshad Number.
Enter a number : 194
194 is not a Harshad Number.
Enter a number : 190
190 is a Harshad Number.
Enter a number : 111
111 is a Harshad Number.
Notes:
-
Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits.
Tags
Java Program To Check For Harshad Number, Java, Math