What is Armstrong Number?
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 the number.
For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371.
Another example is 153, where 1^3 + 5^3 + 3^3 = 153.
These numbers have a unique property that makes them interesting and useful in mathematics and computer science.
In this article, we will explore how to write a Java program to check if a number is an Armstrong number.
Armstrong Number Formula
The mathematical formula to check if a number is an Armstrong number is quite straightforward.
You will use this formula to write your Java program, so it’s important to understand it well.
To determine if a number is an Armstrong number, you need to calculate the sum of its digits raised to the power of the number of digits.
In other words, if you have a number with ‘n’ digits, you will raise each digit to the power of ‘n’ and then add them up.
If the resulting sum is equal to the original number, then it is an Armstrong number.
So, in general, the formula to calculate Armstrong number is
d1^n + d2^n + … + dn^n
where d1, d2, …, dn are the digits of the number, and n is the number of digits.
This formula is the core of your Java program, and you will implement it using a loop to extract each digit from the number, calculate its power, and add it to the sum.
Armstrong Number Logic In Java
Following is the logic based on which we can write Armstrong number program in java.
Step | Description |
---|---|
1 | Declare a variable to store the input number. |
2 | Create a map to store the digits of the number. |
3 | Calculate the power of each digit using a map. |
4 | Check if the sum of the powers is equal to the original number. |
5 | Print the result, indicating whether the number is an Armstrong number or not. |
Armstrong Number Java Program
The following code example demonstrates one way to check if a number is an Armstrong number in Java: java
import java.util.HashMap; import java.util.Map; public class ArmstrongNumber { public static void main(String[] args) { int num = 371; // create a map to store the digits of the number Map<Integer, Integer> digitMap = new HashMap<>(); int temp = num; while (temp > 0) { int digit = temp % 10; digitMap.put(digit, digit); temp /= 10; } // calculate the power of each digit using the map int sum = 0; for (int digit : digitMap.keySet()) { sum += Math.pow(digit, digitMap.size()); } // check if the sum is equal to the original number if (sum == num) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } } }
In this code example, first we initialize a temporary variable temp to hold the value of the input number.
Then, use a while loop to extract each digit from the number by taking the modulus of temp with 10 (which gives you the last digit of the number).
In every iteration, we use a HashMap to store these digits of the input number.
We then iterate over the Map and calculate the power of each digit using Math.pow()
and sum them up.
Finally, we check if the sum is equal to the original number, and print out whether it is an Armstrong number or not.
Armstrong Number Java Program Using Array
You can also use an array to store the digits instead of a Map.
You can then iterate through the array to calculate the power of each digit and sum them up.
Below is the code example
public class ArmstrongNumber { public static void main(String[] args) { int num = 371; // create an array to store the digits of the number int[] digits = new int[String.valueOf(num).length()]; int temp = num; int i = 0; while (temp > 0) { digits[i++] = temp % 10; temp /= 10; } // calculate the power of each digit using the array int sum = 0; for (int digit : digits) { sum += Math.pow(digit, digits.length); } // check if the sum is equal to the original number if (sum == num) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } } }
In this example, we create an array to store the digits of the number.
We then iterate through the array to calculate the power of each digit and sum them up.
This approach is more concise and efficient than using a Map.
Armstrong Number Using Recursion
Recursion is a programming technique where a function calls itself repeatedly until it reaches a base case that stops the recursion.
To check Armstrong numbers using recursion, we need to write a recursive function that takes an integer as an input and returns true if it’s an Armstrong number, and false otherwise.
The function would recursively calculate the sum of the digits raised to the power of the number of digits, and then compare it with the original number.
Below is the program that demonstrates how you can use recursion to check Armstrong number
public class ArmstrongNumber { public static boolean isArmstrong(int num, int temp, int sum, int n) { if (temp == 0) { return sum == num; } int digit = temp % 10; sum += Math.pow(digit, n); return isArmstrong(num, temp / 10, sum, n); } public static void main(String[] args) { int num = 371; int temp = num; int n = String.valueOf(num).length(); if (isArmstrong(num, temp, 0, n)) { System.out.println(num + " is an Armstrong number"); } else { System.out.println(num + " is not an Armstrong number"); } } }
In this example, isArmstrong()
method takes four parameters:
A. the original number,
B. a temporary variable to store the number,
C. the sum of the digits raised to the power of the number of digits, and
D. the number of digits itself.
The method recursively calculates the sum until the temporary variable becomes 0, and then returns true
if the sum is equal to the original number, and false
otherwise.
Output and Explanation
After running the Armstrong number program in Java, you will get an output indicating whether the input number is an Armstrong number or not.
For example, if you run the program with the input value 371, the output will be
371 is an Armstrong number
This is because 371 is indeed an Armstrong number, as the sum of its digits (3^3 + 7^3 + 1^3) equals the original number.
If you run the program with a different input value, such as 123, the output will be:
123 is not an Armstrong number
This is because 123 is not an Armstrong number, as the sum of its digits (1^3 + 2^3 + 3^3) does not equal the original number.
Handling Negative Input Values
For a robust Armstrong number program in Java, you need to consider handling negative input values.
As you know, Armstrong numbers are defined for positive integers, and the mathematical formula to check if a number is an Armstrong number doesn’t apply to negative numbers.
When you run your program with a negative input value, it may produce incorrect results or even throw an exception.
To avoid this, you should add a check at the beginning of your program to handle negative input values.
You can do this by using an if
statement to check if the input number is less than 0.
If it is, you can print an error message and exit the program. Example,
import java.util.HashMap; import java.util.Map; public class ArmstrongNumber { public static void main(String[] args) { int num = -371; if (num < 0) { System.out.println("Input value cannot be negative."); return; } // rest of the code here } }
By adding this simple check, you can ensure that your program behaves correctly and provides a meaningful error message when a negative input value is provided.
Final Words
Conclusively, we learnt a comprehensive understanding of Armstrong numbers, including their definition and mathematical formula.
Moreover, we also learned how to write a Java program to check if a number is an Armstrong number, with code examples.
Hope it was useful.