niven number in java, what is niven number in java

niven number in java, what is niven number in java

A Niven number (also known as a Harshad number) is a positive integer that is divisible by the sum of its digits. To determine if a given number is a Niven number, you can calculate the sum of its digits and then check if the original number is divisible by that sum.

Here's how you can implement a Niven number checker in Java:

```java
public class NivenNumber {
    public static void main(String[] args) {
        int number = 18; // Change this to the number you want to check
        if (isNivenNumber(number)) {
            System.out.println(number + " is a Niven number.");
        } else {
            System.out.println(number + " is not a Niven number.");
        }
    }

    public static boolean isNivenNumber(int n) {
        int sumOfDigits = calculateDigitSum(n);
        return n % sumOfDigits == 0;
    }

    public static int calculateDigitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
}
```

In this example, the `isNivenNumber` function checks whether a given number is a Niven number by calculating the sum of its digits using the `calculateDigitSum` function and then checking if the original number is divisible by the sum of digits. You can change the `number` variable in the `main` method to test other numbers for Niven property.

niven number in java in details 


A Niven number, also known as a Harshad number, is a positive integer that is divisible by the sum of its digits. In other words, a number is considered a Niven number if it's evenly divisible by the sum of its individual digits.

For example, let's take the number 18:
- The sum of its digits is 1 + 8 = 9.
- 18 is divisible by 9 (since 18 ÷ 9 = 2), so it's a Niven number.

In Java, you can check if a given number is a Niven number by calculating the sum of its digits and then checking if the original number is divisible by that sum. Here's a simple Java function to determine if a number is a Niven number:

```java
public class NivenNumberChecker {
    public static void main(String[] args) {
        int number = 18; // Change this to the number you want to check

        if (isNivenNumber(number)) {
            System.out.println(number + " is a Niven number.");
        } else {
            System.out.println(number + " is not a Niven number.");
        }
    }

    public static boolean isNivenNumber(int n) {
        int sumOfDigits = calculateDigitSum(n);
        return n % sumOfDigits == 0;
    }

    public static int calculateDigitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
}
```

In this example, the `isNivenNumber` function checks whether a given number is a Niven number by calculating the sum of its digits using the `calculateDigitSum` function and then checking if the original number is divisible by the sum of digits. You can replace the `number` variable with any positive integer you want to test for Niven property.