In this article, we will discuss and execute a simple Java program to check whether the given number is Armstrong number or not
Armstrong number:
- If the given number is equal to sum of their mth power of each digits then the number is Armstrong number
- where m is length of the given number
- For example, let’s assume given number is 153 and their sum of cubes is (13 + 53 + 33) = (1 + 125 + 27) = 153, so given number 153 is Armstrong number
- Likewise, let’s take another number 1634 and their sum of 4th power of each digits is (14 + 64 + 34 + 44) = (1 + 1296 + 81 + 256) = 1634, so given number 1634 is Armstrong number
- Let’s consider number 1234 and their sum of 4th power of each digits is (14 + 24 + 34 + 44) = (1 + 16 + 81 + 216) = 314, so given number 1234 is not Armstrong number since original number and their sum of mth power isn’t Equal
Armstrong number program steps:
- First assign the original number in local variables for comparison at the end
- Get the length of the given number for calculating their power of each digits in the given number
- Now, iterate through while-loop until given number becomes zero
- Get the remainder after doing modulo of 10 –> which will give last digit of the given number
- Get the new number after dividing with 10 –> this step provide new number after removing last digit
- Inside While-loop, iterate using for-loop starting with 0 till (length-1) for calculating mth power of the remainder
- After completion of for-loop, add the calculated value to the result variable
- Finally compare original given number with the resulting number
- If both comes to be same/Equal then the given number is Armstrong number
- Otherwise it is not Armstrong number
ArmstrongNumber.java
package in.bench.resources.java.armstrong.number;
public class ArmstrongNumber {
// main() method
public static void main(String[] args) {
System.out.println("1.Check for number 153 \n"
+ checkArmstrongNumber(153));
System.out.println("\n2.Check for number 1634 \n"
+ checkArmstrongNumber(1634));
System.out.println("\n3.Check for number 1234 \n"
+ checkArmstrongNumber(1234));
}
/**
* This method checks whether the given number is Armstrong number or not
*
* @param number
* @return
*/
public static String checkArmstrongNumber(int number) {
// local variables
int remainder = 0;
int result = 0;
int length = 0;
// assign original number for comparison at the end
int dividend = number;
// get length of given number
length = String.valueOf(number).length();
// get remainder and quotient by iterating inside while-loop
while(number > 0) {
// get 1st remainder
remainder = number % 10; // 3 5 1
// get 1st quotient
number = number / 10; // 15 1 0
int tempMul = 1;
for(int index=0; index<length; index++) {
//
tempMul = tempMul * remainder; // 3 9 27 // 5 25 125 // 1 1 1
}
// result
result = result + tempMul; // 27 + 125 + 1
}
if(dividend == result) {
return dividend + " is a Armstrong number";
}
else {
return dividend + " is not a Armstrong number";
}
}
}
Output:
1.Check for number 153
153 is a Armstrong number
2.Check for number 1634
1634 is a Armstrong number
3.Check for number 1234
1234 is not a Armstrong number
Note : There are various other ways to check whether the given number is Armstrong number or not
Related Articles :
- Java – To print first N natural numbers
- Java – Swapping two numbers without temporary variable
- Java – Swapping two numbers using third or temporary variable
- Java – Check whether the given number is Armstrong number or Not ?
- Java – Check whether number is Positive or Negative or Zero ?
- Java – Check whether number is Even or Odd ?
- Java – How to Reverse a Number in different ways ?
- Java – Check whether number is Prime or Not ?
- Java – Print Prime numbers between specified range or interval
- Java – How to find/remove first and last digit of a number ?
- Java – How to generate Fibonacci numbers using Stream ?
- Java – How to convert Roman letter/numbers to Integer ?
- Java – How to convert Celsius to Fahrenheit and vice-versa ?
Happy Coding !!
Happy Learning !!