## Example Program – Armstrong Number

Below sample programs finds whether the given number is an Armstrong Number or not.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Few more Examples for the Armstrong numbers are : 153, 371, 9474, 54748.

package in.javatutorials;

/**
* @author malliktalksjava.in
*
*/
public class ArmstrongNumber {

/**
* @param inputNumber
* @return boolean true/false will be return
*/
public static boolean isArmstrong(int inputNumber) {
String inputAsString = inputNumber + “”;
int numberOfDigits = inputAsString.length();
int copyOfInput = inputNumber;
int sum = 0;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
copyOfInput = copyOfInput / 10;
}
if (sum == inputNumber) {
return true;
} else {
return false;
}
}

/**
* @param args
*/
public static void main(String[] args) {
int inputNumber = 153;
System.out.print(“Enter a number: “);
boolean result = isArmstrong(inputNumber);
if (result) {
System.out.println(inputNumber + ” is an armstrong number”);
} else {
System.out.println(inputNumber + ” is not an armstrong number”);
}
}
}