Niven Number in JavaNiven 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
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
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. ConclusionHarshad 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. Next TopicRhombus Pattern in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India