-
Table of Contents
4 DIGIT ARMSTRONG NUMBER IN JAVA
Armstrong numbers, also known as narcissistic numbers, are numbers that are equal to the sum of their own digits raised to the power of the number of digits. In this article, we will explore how to find 4-digit Armstrong numbers in Java.
What is an Armstrong Number?
An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Finding 4-Digit Armstrong Numbers in Java
In order to find 4-digit Armstrong numbers in Java, we can write a simple program that iterates through all 4-digit numbers and checks if they are Armstrong numbers. Here is a sample Java program to find 4-digit Armstrong numbers:
“`java
public class ArmstrongNumber {
public static void main(String[] args) {
for (int i = 1000; i 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
if (sum == i) {
System.out.println(i + ” is an Armstrong number.”);
}
}
}
}
“`
Example Output
When you run the above program, it will output all 4-digit Armstrong numbers. Here are some examples:
- 1634 is an Armstrong number.
- 8208 is an Armstrong number.
- 9474 is an Armstrong number.
Conclusion
In conclusion, finding 4-digit Armstrong numbers in Java is a simple task that involves iterating through all 4-digit numbers and checking if they are Armstrong numbers.
. By understanding the concept of Armstrong numbers and implementing a program to find them, you can enhance your programming skills and deepen your understanding of number theory.
For more information on Armstrong numbers and their properties, you can visit this Wikipedia page.