In simple terms, 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 in it.
For example, let's consider the number 153. It has 3 digits. If we raise each digit to the power of 3 (the number of digits) and sum them up, we get:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Since the sum of the digits raised to the power of the number of digits equals the original number (153), it is an Armstrong number.
Here's an example code in Java to check if a given number is an Armstrong number or not:
public class ArmstrongNumberExample {
public static void main(String[] args) {
int number = 153;
boolean isArmstrong = isArmstrongNumber(number);
if (isArmstrong) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
public static boolean isArmstrongNumber(int number) {
int originalNumber = number;
int sum = 0;
int digits = String.valueOf(number).length();
while (number != 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
return sum == originalNumber;
}
}
No comments:
Post a Comment