Niven Number Program in JavaNiven numbers are named after Ivan Niven, a Canadian mathematician who introduced them in a paper in 1977. However, they were first studied by the Indian mathematician D. R. Kaprekar in the 1950s. In this section, we will learn what is niven number with example and niven number Java Program. Niven NumberA Niven number is a positive integer that is divisible by the sum of its digits. It is also called as Harshad number. Properties of Niven NumberNiven numbers have some interesting properties. For example, the sum of the digits of any Niven number is always a Niven number. Also, the average of the first n Niven numbers is always equal to (n + 1)/2. Uses of Niven NumberNiven numbers are not very common, but they do appear in a variety of mathematical contexts. For example, they are used in some algorithms for generating random numbers. They are also used in some number theory problems, such as finding the prime factorization of a large number. Example of Niven NumberConsider the number 18 and check it is niven number or not. Sum of the digits of the number = 1 + 8 = 9. So, 18 is divisible by 9. Hence, 18 is a Niven number. Steps to find Niven Number
Therefore, the overall time complexity of the algorithm is O(log n). Algorithm: Without using String
Let's implement the above approach in a Java program. Java Program to Find the Niven NumberNivenNumber.java Output: Given number:21 21 is a niven number Time Complexity The time complexity of the given code is O(log n), where n is the number of digits in the number being checked. The while loop in the code iterates over the digits of the number, and the number of iterations is equal to the number of digits in the number. The number of digits in a number is logarithmic to the number itself, so the time complexity of the loop is O(log n). The rest of the code in the function takes constant time, so the overall time complexity of the function is O(log n). Algorithm 2 With using String
NivenNumberEaample.java Output: Given number:40 40 is a niven number The time complexity of the given code is also O(log n), where n is the number of digits in the number being checked.
Next TopicPassing Object to Method in Java
