Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
0
Indexable
Never
Class Number: 
 public class Number { 
    private double num; 
     public Number(double num) { 
        this.num = num; 
    } 
     public boolean isZero() { 
        return num == 0; 
    } 
     public boolean isPositive() { 
        return num > 0; 
    } 
     public boolean isNegative() { 
        return num < 0; 
    } 
     public boolean isOdd() { 
        return num % 2 != 0; 
    } 
     public boolean isEven() { 
        return num % 2 == 0; 
    } 
     public boolean isPrime() { 
        if (num <= 1) { 
            return false; 
        } 
        for (int i = 2; i <= Math.sqrt(num); i++) { 
            if (num % i == 0) { 
                return false; 
            } 
        } 
        return true; 
    } 
     public boolean isAmstrong() { 
        int n = (int) num; 
        int temp = n; 
        int sum = 0; 
        while (temp > 0) { 
            int rem = temp % 10; 
            sum += Math.pow(rem, 3); 
            temp /= 10; 
        } 
        return sum == n; 
    } 
     public double getFactorial() { 
        if (num < 0) { 
            return -1; 
        } 
        double result = 1; 
        for (int i = 1; i <= num; i++) { 
            result *= i; 
        } 
        return result; 
    } 
     public double getSqrt() { 
        return Math.sqrt(num); 
    } 
     public double getSqr() { 
        return num * num; 
    } 
     public double sumDigits() { 
        int n = (int) num; 
        int sum = 0; 
        while (n > 0) { 
            int rem = n % 10; 
            sum += rem; 
            n /= 10; 
        } 
        return sum; 
    } 
     public double getReverse() { 
        int n = (int) num; 
        int rev = 0; 
        while (n > 0) { 
            int rem = n % 10; 
            rev = rev * 10 + rem; 
            n /= 10; 
        } 
        return rev; 
    } 
     public void listFactor() { 
        for (int i = 1; i <= num; i++) { 
            if (num % i == 0) { 
                System.out.print(i + " "); 
            } 
        } 
        System.out.println(); 
    } 
     public void dispBinary() { 
        int n = (int) num; 
        String binary = ""; 
        while (n > 0) { 
            int rem = n % 2; 
            binary = rem + binary; 
            n /= 2; 
        } 
        System.out.println(binary); 
    } 
}