Java Program to check for Harshad Number: ** What is a Harshad Number?** Harshad Number also called Niven Number is a number that is **divisible by the sum of its digits**.

*Example:*

- Number 200 is a Harshad Number because sum of digits 2 and 0 and 0 is 2(2+0+0) and 200 is divisible by 2.
- Number 171 is a Harshad Number because sum of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.
- Number 36 is a Harshad Number because the sum of the digits 3 and 6 is 9(3+6) and 36 is divisible by 9.

Some examples of Harshad Number Series are:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112…

import java.util.Scanner; public class Harshad { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // defines Scanner System.out.print("Please enter a positive integer (>=1): "); // asking for input int input = sc.nextInt(); // loop for wrong input while (input >= 1) { if (input <=0) { System.out.println("Sorry, the integer must be greater than or equal to 1."); System.out.print("Please enter a positive integer (>=1): "); } int s=0; //finding sum of digits if (input > 0) { int p = input % 10; s = s + p; input = input / 10; int answer = input % s; // put in equation for Harshad number. if ( answer == 0) { System.out.println(input + " is a Harshad number."); } else { System.out.println(input + " is not a Harshad number."); } } } } }

#### Output:

Please enter a positive integer (>=1): 18

18 is a Harshad Number.Please enter a positive integer (>=1): 0

Sorry, the integer must be greater than or equal to 1.

Please enter a positive integer (>=1): 20

20 is not a harshad number.Please enter a positive integer (>=1): 201

201 is a Harshad Number.import java.util.*; public class Harshad { public static void main(String[] args) { long seq = 200; //limit the seq of harshad numbers int n, s; for (int i = 1; i <= seq; i++) { n = i; s = 0; while (n != 0) { s += n % 10; n = n / 10; } if (i % s == 0) { System.out.println(i); // Thus a Harshad Number } } } }

#### Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200.