Tuesday, 18 July 2017 06:25

## Java program to check for Harshad Number (Niven Number)

Written by
Rate this item

#### Question:

Write a Program in Java to input a number and check whether it is a Harshad Number or Niven Number or not..

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.

Let’s understand the concept of Harshad Number through the following example:

• The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)
• The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2 and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)
• The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9 is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)

The first few Harshad numbers in base 10 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, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.

#### Programming Code:

 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 `/**` `* The class HarshadNumber inputs a number and checks if it a Harshad Number or not` `* @author : www.anishsir.in` `* @Program Type : BlueJ Program - Java` `*/`   `import` `java.util.*;` `class` `HarshadNumber` `{` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``Scanner sc = ``new` `Scanner(System.in);` `        `  `        ``System.out.print(``"Enter a number : "``);` `        ``int` `n = sc.nextInt();` `        ``int` `c = n, d, sum = ``0``;` `        `  `        ``//finding sum of digits` `        ``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."``);      ` `    ``}` `}`

#### 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.`