How to Check The Armstrong Numbers in Java?
The “Armstrong number” provides insights into number patterns and mathematical properties. It helps in understanding number theory concepts and exploring self-referential relationships within numbers. In addition, it helps in ensuring the accuracy of data or user input. It can be useful where input integrity is crucial.
This blog helps in finding the Armstrong number for provided data.
How to Check Armstrong Numbers in Java?
The “Armstrong number” is checked to identify numbers that satisfy a specific mathematical property. If the provided value is the same as the addition of its own integers raised to the power of the total count of integers of the provided value.
Now, let us visit some examples for more understanding:
Example 1: Identifying the Armstrong Numbers
Visit the program to check if the provided number is Armstrong number or not:
//Importing Required Utilities.
public class ArmstrongChecker {
public static void main(String[] args)
//Declaring the main() method
{
Scanner demoTest = new Scanner(System.in);
System.out.print("Enter number to check: ");
int numEle = demoTest.nextInt();
int origNum = numEle;
int digits = 0;
while (origNum != 0) {
origNum /= 10;
digits++;
}
int sum = 0;
int temp = numEle;
for (int i = 0; i < digits; i++) {
int dig = temp % 10;
sum += Math.pow(dig, digits);
temp /= 10;
}
if (sum == numEle) {
System.out.println(numEle + " is an Armstrong number.");
} else {
System.out.println(numEle + " does not Satisfy Condition for Armstrong Number.");
}
}
}
Description of the above code:
-
- First, the object for the “Scanner” class is created to retrieve an integer from the end user using the “nextInt()” method and store the retrieved data in a variable named “numEle”.
- Next, this retrieved value is assigned to the int type variable named “origNum” and initializes a variable named “digits” with “0”.
- Then, the “while” loop is used that repeatedly divides origNum by 10 and increments the digits variable each time until origNum becomes 0.
- After that, declare a variable “sum” and set the value of “numEle” to the “temp” variable. And utilizes the “for” loop that iterates till the “digits” variable value.
- And in each “for” loop iteration, the last digit of “temp” is extracted using the modulus operator and stored in a new variable “dig”. Then, the cube of the digit is then added to the “sum” variable using the Math.pow() method.
- In the end, the “if/else” statement is utilized to determine whether the calculated sum is equal to the original provided number by the user. If both values are equal, then the provided number is an Armstrong number and vice versa.
After the compilation:
The output shows that the provided number is an Armstrong number.
Example 2: Find All Residing Armstrong Numbers Within the Provided Limit
To find all the Armstrong numbers to the provided value or limit, visit the below code:
import java.lang.Math;
public class ArmstsrongNumberExample
{
static boolean isArmsNum(int j) {
int buff, singDig=0, end=0, calc=0;
buff=j;
while(buff>0) {
buff = buff/10;
singDig++;
}
buff = j;
while(buff>0)
{
end = buff % 10;
calc += (Math.pow(end, singDig));
buff = buff/10;
}
if(j==calc)
return true;
else return false;
}
public static void main(String args[])
//Declaring the main() method
{
int proNum;
Scanner sc= new Scanner(System.in);
System.out.print("Insert the Upper Limit: ");
proNum=sc.nextInt();
System.out.println("Armstrong Number up to provided Limit "+ proNum + " are: ");
for(int k=0; k<=proNum; k++)
if(isArmsNum(k))
System.out.print(k+ ", ");
}
}
Explanation of the above code:
-
- First, declare multiple variables “buff”, “singDig”, “end”, and “calc” having a type of “int”, inside the boolean type method named “isArmsNum()”. It receives a parameter that is assigned to the variable named “buff”.
- Next, the “while” loop is declared which iterates till the buff value reaches “0”. After that, the “buff” is modulus by “10” to remove the last digit from the provided value and increments the “singDig” variable.
- Then, the “while” loop is utilized again over the “buff” variable to extract the last digit. The cube of the digit is calculated by utilizing the “Math.pow()” method and then added to the “calc” variable.
- Now, the “if” statement is utilized to check whether the calculated value in the “calc” variable is equal to the provided value by the end-user or not. Also, display the message accordingly.
- After that, the input from the end user is retrieved with the help of the “Scanner” utility in the “main()” method.
- In the end, the “for” loop is utilized that iterates till the provided value, and each iteration is called the “isArmsNum()” method. This method receives all values till the provided value is reached and checks each value for the Armstrong number.
After the compilation:
The output displays all Armstrong numbers till the provided value which is “370” in this case.
Conclusion
To find an Armstrong number, first, count the digits the provided number contains. Then, extract each digit from the provided number one by one with the help of modulus and division operations. Next, raise each integer of the value to the power of the total number of integers and add the resulting value to a new variable. Finally, check if the obtained variable value is equal to the provided number, if it is equal then the provided number is an Armstrong number, otherwise not.
Source: linuxhint.com