From 63d5f2ae230117901f39405d923d3996da9fc20e Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 2 Sep 2024 01:25:58 +0530 Subject: [PATCH] Update OOP/Java/Lab/Week5/Number.java --- OOP/Java/Lab/Week5/Number.java | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/OOP/Java/Lab/Week5/Number.java b/OOP/Java/Lab/Week5/Number.java index 40c2044..a371df3 100644 --- a/OOP/Java/Lab/Week5/Number.java +++ b/OOP/Java/Lab/Week5/Number.java @@ -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());