Java With Us
 Home   |   Tutorial   |   Programs

# Armstrong Numbers - Java Program

An Armstrong number is the one that equals the sum of its digits raised to the power of the number of digits in that number which is to be checked. To be more clear, let the number be n and the number of digits be x. We represent the number as nxnx-1nx-2...n3n2n1 where n1, n2, n3...nx are single digits 0-9. n is an Armstrong number if

n1x + n2x + n3x + nx-1x + nxx = n

153, 371, 9474 and 54748 are few Armstrong numbers.

153 = 13 + 53 + 33
371 = 33 +73 +13
9474 = 94 + 44 +74 + 44
54748 = 55 + 45 + 75 + 45 + 85

## Java Program to check whether a given number is an Armstrong number

Let the number that is to be checked be stored in an int variable input. We make a copy of that input and store it in copyOfInput. It is required that we make a copy because the number to be checked will be changed during the execution of the program and towards the end, we need the original input number to declare whether it is an Armstrong number or not.

We first find the number of digits in the input number. To do so, we construct a String by concatenating the input number with a null string. We can also use the valueOf(int) method of the String class. The length of the resultant string obtaining using the length() method will give us the number of digits in the input number.

We also declare a variable sum and initialise it to zero. This variable will hold the sum of the digits raised to the appropriate powers.

Next, in a while loop, we extract the digits of the input from right to left, raise them to the appropriate power and add to the existing value of sum. To extract the last digit of the number, we find the remainder obtained don dividing that number by 10 using the modulo operator. The remainder is raised to the power numberOfDigits using the Math.pow() function. This function returns a double value which we explicitly cast to an int so that we can add it to the existing sum. Now, the last digit of copyOfInpuit will be truncated by diving it with 10. The while loop executes as long as copyOfInput is not zero. When it is zero, all the digits would have been processed.

Finally, we check if sum equals the input number. If it is so, the number is an Armstrong number. Given below is the complete code for this program.

import java.util.Scanner;

public class ArmstrongNumber {

public static boolean isArmstrong(int input) {
String inputAsString = input + "";
int numberOfDigits = inputAsString.length();
int copyOfInput = input;
int sum = 0;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
copyOfInput = copyOfInput / 10;
}
if (sum == input) {
return true;
} else {
return false;
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputNumber = scanner.nextInt();
boolean result = isArmstrong(inputNumber);
if (result) {
System.out.println(inputNumber + " is an armstrong number");
} else {
System.out.println(inputNumber + " is not an armstrong number");
}
}
}

Here are two sample executions of the program.

Enter a number: 153
153 is an armstrong number
Enter a number: 3479
3479 is not an armstrong number

## Java Program to print Armstrong Numbers

Here is another Java program which takes a start and an end number and prints all the Armstrong numbers that lie between the above two numbers.

import java.util.Scanner;

public class ArmstrongNumber {

public static boolean isArmstrong(int input) {
String inputAsString = input + "";
int numberOfDigits = inputAsString.length();
int copyOfInput = input;
int sum = 0;
while (copyOfInput != 0) {
int lastDigit = copyOfInput % 10;
sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
copyOfInput = copyOfInput / 10;
}
if (sum == input) {
return true;
} else {
return false;
}
}

public static void printAll(int start, int end) {
System.out.println("List of Armstrong Numbers between " + start + " and " + end + " :");
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
System.out.println(i);
}
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter start number: ");
int start = scanner.nextInt();
System.out.print("Enter end number: ");
int end = scanner.nextInt();
printAll(start, end);
}
}

Here is a sample execution

Enter start number: 1
Enter end number: 3479
List of Armstrong Numbers between 1 and 3479 :
1
2
3
4
5
6
7
8
9
153
370
371
407
1634