Update OOP/Java/Lab/Week5/Number.java
This commit is contained in:
parent
b178260bf8
commit
63d5f2ae23
@ -29,40 +29,24 @@ class Number {
|
||||
}
|
||||
|
||||
public boolean isPrime() {
|
||||
if (value <= 1 || value != (int) value) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 2; i <= Math.sqrt(value); i++) {
|
||||
if ((int) value % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (value <= 1 || value != (int) value) return false;
|
||||
for (int i = 2; i <= Math.sqrt(value); i++) if (
|
||||
(int) value % i == 0
|
||||
) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isArmstrong() {
|
||||
if (value != (int) value) {
|
||||
return false;
|
||||
}
|
||||
int num = (int) value;
|
||||
int originalNum = num;
|
||||
int sum = 0;
|
||||
int digits = String.valueOf(num).length();
|
||||
|
||||
while (num > 0) {
|
||||
int digit = num % 10;
|
||||
sum += Math.pow(digit, digits);
|
||||
num /= 10;
|
||||
}
|
||||
|
||||
return sum == originalNum;
|
||||
if (value != (int) value) return false;
|
||||
int num = (int) value, sum = 0, digits = String.valueOf(num).length();
|
||||
for (int n = num; n > 0; n /= 10) sum += Math.pow(n % 10, digits);
|
||||
return sum == num;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.print("Enter a number: ");
|
||||
double input = sc.nextDouble();
|
||||
Number number = new Number(input);
|
||||
Number number = new Number(sc.nextDouble());
|
||||
|
||||
System.out.println("Is Zero: " + number.isZero());
|
||||
System.out.println("Is Positive: " + number.isPositive());
|
||||
|
Loading…
Reference in New Issue
Block a user