isSelfDivisor java

https:‮ww//‬w.lautturi.com
isSelfDivisor java

To check if a number is self-divisor in Java, you can use the following approach:

public static boolean isSelfDivisor(int n) {
    int temp = n;
    while (temp > 0) {
        int digit = temp % 10;
        if (digit == 0 || n % digit != 0) {
            return false;
        }
        temp /= 10;
    }
    return true;
}

This function uses a loop to extract each digit of the number n, and checks if the number is divisible by each digit. If any of the digits is 0 or if the number is not divisible by any of the digits, it returns false because n is not a self-divisor. If all of the digits are non-zero and the number is divisible by all of the digits, it returns true because n is a self-divisor.

Here is an example of how to use this function:

System.out.println(isSelfDivisor(128)); // Outputs true
System.out.println(isSelfDivisor(212)); // Outputs true
System.out.println(isSelfDivisor(222)); // Outputs true
System.out.println(isSelfDivisor(257)); // Outputs false
System.out.println(isSelfDivisor(291)); // Outputs false

In this example, the function isSelfDivisor() is called with the numbers 128, 212, 222, 257, and 291 as arguments. It returns true for the self-divisor numbers 128, 212, and 222, and false for the non-self-divisor numbers 257 and 291.

For more information on self-divisor numbers and algorithms for checking if a number is self-divisor, you can refer to online resources or textbooks on mathematics or computer science.

Created Time:2017-11-01 22:29:53  Author:lautturi