Introduction
A number is said to be an Armstrong Number in Java, if it is a positive number and it is equal to the addition of cubes of its digits.
Example of Armstrong numbers : 0, 1, 153, 370, 371, 407 etc.
153 is said to be an Armstrong Number as :
153 = (1*1*1) + (5*5*5) + (3*3*3) 153 = 1 + 125 + 27 153 = 153
370 is said to be an Armstrong Number as :
370 = (3*3*3) + (7*7*7) + (0*0*0) 370 = 27 + 343 + 0 370 = 370
How to check Armstrong Number in Java
We have understood what is armstrong number. Now, Let’s take a look at different examples explained in below section to learn :
- Program to check Armstrong Number for 3 digits.
- Program to check Armstrong Number for n digits.
Example 1 : Program to check Armstrong Number in Java for a 3 digit number
public class CheckArmstrongNumber {
public static void main(String[] args) {
int num = 153, original, rem, output = 0;
original = num;
while (original != 0)
{
rem = original % 10;
output += Math.pow(rem, 3);
original /= 10;
}
if(output == num){
System.out.println(num + " is an Armstrong number");
}
else{
System.out.println(num + " is not an Armstrong number");
}
}
}
Output :
153 is an Armstrong number
Explanation
First, we have stored the provided number (num) value in another variable which is original.
As we will check the current value with the calculated value at the end to check if the number is armstrong or not.
Then , we have used a while loop. This loop is running on condition until original is not 0.
During each iteration of this loop, the remainder is powered by three. We are doing this calculation using Math.pow() function of Java.
The calculated value is then being added to the output in each iteration.
Finally, we are removing the last digit from original after division by 10.
Now if the value original and output is equal then the provided number is armstrong number otherwise it is not.
Example 2 : Program to check Armstrong Number in Java for n digit number
public class CheckArmstrongNumber {
public static void main(String[] args) {
int num = 1634, original, rem, output = 0, n = 0;
original = num;
for (;original != 0; original /= 10, ++n);
original = num;
for (;original != 0; original /= 10)
{
rem = original % 10;
output += Math.pow(rem, n);
}
if(output == num){
System.out.println(num + " is an Armstrong number");
}
else{
System.out.println(num + " is not an Armstrong number");
}
}
}
Output :
1634 is an Armstrong number
Explanation
In this example , we are using for loops.
First for loop is counting the number of digits in the provided number.
Second for loop is calculating the result in each iteration as remainder is powered by the number of digits n.
Example 3 : Program to check Armstrong Number in Java with user input
import java.util.Scanner;
public class CheckArmstrongNumber {
public static void main(String[] args) {
int num , original, rem, output = 0;
System.out.println("Enter a 3 Digit Number to check :");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
scanner.close();
original = num;
while (original != 0)
{
rem = original % 10;
output += Math.pow(rem, 3);
original /= 10;
}
if(output == num){
System.out.println(num + " is an Armstrong number");
}
else{
System.out.println(num + " is not an Armstrong number");
}
}
}
Output :
Enter a 3 Digit Number to check : 153 153 is an Armstrong number
Further Readings :
- String and Number Palindrome Program in Java.
- How to find duplicate characters in a String.
- Program for Fibonacci series in Java.
Conclusion
We have learnt what is armstrong number and the program to check if the number is armstrong number or not. This is one of the basic but most asked Java interview Question.
Thanks for Reading!