In this blog, we will be discussing how to check whether a given number is an Armstrong number or not using Java.
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 because 1^3 + 5^3 + 3^3 = 153.
To check if a number is an Armstrong number, we will use the following steps:
- Convert the given number to a string
- Find the length of the string
- Initialize a variable to store the sum of the digits raised to the power of the length of the string
- Iterate through the string, convert each character to an integer, and add it to the sum raised to the power of the length of the string
- Compare the sum with the original number
- If the sum is equal to the original number, the number is an Armstrong number. If not, it is not an Armstrong number.
Java Program
Here is the Java code for this process:
public class ArmstrongNumber {
public static void main(String[] args) {
int num = 153;
int originalNum = num;
int sum = 0;
int length = String.valueOf(num).length();
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, length);
num /= 10;
}
if (sum == originalNum) {
System.out.println(originalNum + " is an Armstrong number.");
} else {
System.out.println(originalNum + " is not an Armstrong number.");
}
}
}In this code, we first define the variable num as 153, which is an Armstrong number. We then store the original value of num in a separate variable originalNum for later comparison. We initialize the sum variable to 0 and find the length of the number using the length() method of the String class.
We then use a while loop to iterate through the digits of the number. In each iteration, we find the last digit of the number using the modulus operator, raise it to the power of the length of the number, and add it to the sum variable. We then divide the number by 10 to get the next digit.
After the while loop, we compare the sum variable with the originalNum variable. If they are equal, the number is an Armstrong number and we print a message saying so. If not, we print a message saying the number is not an Armstrong number.
In this example, the output will be “153 is an Armstrong number.”
In conclusion, we have learned how to check whether a given number is an Armstrong number or not using Java. This process can be used to check for Armstrong numbers in a variety of applications and can be modified to work with larger numbers as well.
Latest Posts:
- Count Subarrays with Odd SumTitle: Count Subarrays with Odd Sum In this guide, you’ll learn how to count the subarrays of an integer array… Read more: Count Subarrays with Odd Sum
- Count Subarrays Whose Product is Less than KTitle: Count Subarrays Whose Product is Less than K In this guide, you’ll learn how to count the contiguous subarrays… Read more: Count Subarrays Whose Product is Less than K
- Minimum Number of Increments to Make Array Strictly IncreasingTitle: Minimum Number of Increments to Make Array Strictly Increasing In this guide, we will solve a classic greedy array… Read more: Minimum Number of Increments to Make Array Strictly Increasing
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together



