This class checks whether a given number is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For example, 370 is an Armstrong number because 3^3 + 7^3 + 0^3 = 370. 1634 is an Armstrong number be
| 13 | * @modifier rahul katteda - (13/01/2025) - [updated the logic for getting total number of digits] |
| 14 | */ |
| 15 | public class Armstrong { |
| 16 | |
| 17 | /** |
| 18 | * Checks whether a given number is an Armstrong number or not. |
| 19 | * |
| 20 | * @param number the number to check |
| 21 | * @return {@code true} if the given number is an Armstrong number, {@code false} otherwise |
| 22 | */ |
| 23 | public boolean isArmstrong(int number) { |
| 24 | if (number < 0) { |
| 25 | return false; // Negative numbers cannot be Armstrong numbers |
| 26 | } |
| 27 | long sum = 0; |
| 28 | int totalDigits = (int) Math.log10(number) + 1; // get the length of the number (number of digits) |
| 29 | long originalNumber = number; |
| 30 | |
| 31 | while (originalNumber > 0) { |
| 32 | long digit = originalNumber % 10; |
| 33 | sum += (long) Math.pow(digit, totalDigits); // The digit raised to the power of total number of digits and added to the sum. |
| 34 | originalNumber /= 10; |
| 35 | } |
| 36 | |
| 37 | return sum == number; |
| 38 | } |
| 39 | } |
nothing calls this directly
no outgoing calls
no test coverage detected