Javatpoint Logo
Javatpoint Logo

Niven Number in Java

Niven Numbers, also known as Harshad Numbers, are fascinating mathematical entities that hold significance in number theory. A Niven Number is a positive integer that is divisible by the sum of its digits. In this article, we will explore the concept of Niven Numbers, delve into the underlying principles, and demonstrate how to implement a Java program to identify these intriguing numbers.

Understanding Niven Numbers:

Let's take a positive integer as an example, say 18. The sum of its digits is 1 + 8 = 9. Now, if 18 is divisible by 9, it is classified as a Niven Number. In this case, 18 is indeed divisible by 9, making it a Niven Number.

To check whether a number is a Niven Number or not, we need to perform the following steps

  1. Convert the positive integer into individual digits.
  2. Sum up the digits.
  3. Check if the original number is divisible by the sum calculated in Step 2.

For instance, consider the number 126. The sum of its digits is 1 + 2 + 6 = 9. As 126 is divisible by 9, it is a Niven Number.

Properties of Niven Numbers

  1. Divisibility Rule: A positive integer is a Niven Number if it is divisible by the sum of its digits. This divisibility property is the defining characteristic of Niven Numbers.
  2. A sequence of Niven Numbers: The sequence of Niven Numbers begins with 1, as all single-digit positive integers are Niven Numbers (since their sum is equal to themselves). The sequence continues with 18, 27, 36, 45, 54, 63, 72, 81, 90, and so on.
  3. Count of Niven Numbers: The count of Niven Numbers increases as we explore higher numbers. The density of Niven Numbers in the set of positive integers is not well understood and is a topic of ongoing research.
  4. Niven's Constant: The sum of the reciprocals of all Niven Numbers converges to a value called Niven's constant. The exact value of Niven's constant is approximately 1.7052111401053678. This constant has connections to the concept of harmonic numbers in mathematics.

Java Implementation:

Let's now dive into the Java implementation of a program to identify Niven Numbers:

NivenNumberChecker.java

Explanation:

We start by importing the Scanner class to read user input from the console.

Next, we define the calculateSumOfDigits function to find the sum of digits of a given number. The function iterates through the digits using a while loop and adds each digit to the sum variable.

The isNivenNumber function takes an integer number as input and returns a boolean value indicating whether it is a Niven Number or not. It calls the calculateSumOfDigits function to find the sum of digits and then checks if the original number is divisible by the sum of its digits.

In the main() method, we prompt the user to enter a positive integer and read the input using the Scanner.

We check if the input is a positive integer. If not, we display an error message. Otherwise, we call the isNivenNumber function to check if the number is a Niven Number and display the result accordingly.

Conclusion

Harshad Numbers, also known as Niven Numbers, are fascinating mathematical ideas with real-world applications in many different disciplines. We described the concept of Niven Numbers in this post and provided a Java program to check the status of a given integer as a Niven integer. You can explore the world of Niven Numbers and delve further into the interesting field of number theory by implementing and playing with this program.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA