Understanding and Implementing Armstrong Numbers in Java

To determine whether a number is an Armstrong number, we follow these steps:


  • Notice: Undefined index: share_to in /var/www/uchat.umaxx.tv/public_html/themes/wowonder/layout/blog/read-blog.phtml on line 41
    :

An Armstrong number, also known as a narcissistic 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, 153 is an armstrong number in java:

1
3
+
5
3
+
3
3
=
1
+
125
+
27
=
153
1
3
+5
3
+3
3
=1+125+27=153
Find the number of digits in the number.
Extract each digit and compute its power raised to the total number of digits.
Sum these powered values.
If the sum equals the original number, it is an Armstrong number.
Java Implementation
Below is a simple Java program to check if a given number is an Armstrong number:

java
Copy
Edit
import java.util.Scanner;

public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

if (isArmstrong(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}

scanner.close();
}

public static boolean isArmstrong(int num) {
int originalNum = num, sum = 0;
int numDigits = String.valueOf(num).length();

while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, numDigits);
num /= 10;
}

return sum == originalNum;
}
}
Explanation of the Code
We take user input using Scanner.
The isArmstrong() method computes whether the number is Armstrong by:
Finding the number of digits.
Extracting each digit and raising it to the power of the digit count.
Summing the results and comparing them with the original number.
If the computed sum matches the original number, it is an Armstrong number.
Example Outputs
Input:
yaml
Copy
Edit
Enter a number: 9474
Output:
csharp
Copy
Edit
9474 is an Armstrong number.
Input:
css
Copy
Edit
Enter a number: 123
Output:
csharp
Copy
Edit
123 is not an Armstrong number.
Checking Armstrong Numbers in a Range
If you want to find all Armstrong numbers in a given range (e.g., 1 to 10000), modify the program to iterate over the range:

java
Copy
Edit
public class ArmstrongRange {
public static void main(String[] args) {
int start = 1, end = 10000;

System.out.println("Armstrong numbers between " + start + " and " + end + ":");
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
System.out.print(i + " ");
}
}
}

public static boolean isArmstrong(int num) {
int originalNum = num, sum = 0;
int numDigits = String.valueOf(num).length();

while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, numDigits);
num /= 10;
}

return sum == originalNum;
}
}
Conclusion
Armstrong numbers are an interesting mathematical concept that can be easily implemented in Java using loops and mathematical operations. This concept is often used in coding challenges and programming exercises to strengthen understanding of loops, conditionals, and number manipulation.

6 Puntos de vista

Lee mas..


Warning: mysqli_query(): (HY000/1114): The table '/tmp/#sql_2863_0' is full in /var/www/uchat.umaxx.tv/public_html/assets/includes/functions_three.php on line 1160

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in /var/www/uchat.umaxx.tv/public_html/assets/includes/functions_three.php on line 1162