Javatpoint Logo
Javatpoint Logo

Niven Number Program in Java

Niven 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 Number

A 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 Number

Niven 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 Number

Niven 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 Number

Consider 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

  • It calculates the sum of the digits of the number. The operation takes O(log n) time, where n is the number of digits in the number.
  • It checks if the number is divisible by the sum of its digits. The operation takes O(1) time.

Therefore, the overall time complexity of the algorithm is O(log n).

Algorithm: Without using String

  • Start by obtaining an integer number. It can be done either by initializing a number or by taking user input.
  • Iterate through each digit of the number.
  • Find the sum of all the digits of the number.
  • After finding the sum of all the digits, check if the original number is divisible by the sum. If the original number is divisible by the sum of its digits, the number is a Niven number, else not a niven number.

Let's implement the above approach in a Java program.

Java Program to Find the Niven Number

NivenNumber.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

  • Get an integer number either by initialization or by user input.
  • Convert the integer to a String using the toString() method.
  • Find the length of the String using the length() method.
  • Iterate through each character of the String using a for loop, convert each character to an integer using charAt(i)-'0', and keep a track of the sum of all digits.
  • Check if the original number is divisible by the sum of all digits. If it is divisible, then the given number is a Niven number. Otherwise, it is not a Niven number.

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.







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