Java program to check for an Armstrong Number
This tutorial provides the java implementation for Armstrong Number determination. It first explains what exactly is an Armstrong number, then provides the java code to check whether a given number is an Armstrong number or not, and finally provides a detailed step-by-step explanation of the java code.
What is Armstrong Number
Any number which equals the sum of each of its digits raised to the power of the total number of digits is an Armstrong number.
Other names for Armstrong number - Narcissistic number, pluperfect digital invariant, plus perfect number.
To check a given number for an Armstrong number the following steps need to be followed -
Applying the above given 4 steps -
Java code to determine whether given number is Armstrong or not
OUTPUT for different input values
Explanation of the code
- Count the total number of digits in the given number. Lets say it is 'n'.
- Separate the digits of the number. Lets say the number is - 'd1 d2...dn' where dk is the digit at position 'k'.
- Calculate the sum of each of the digits raised to the total count of digits. This will be - (d1)n+(d2)n...+(dn)n.
- If the given number equals the sum obtained in step-3, then its an Armstrong number, else not.
Applying the above given 4 steps -
- Number of digits in 370 - 3.
- Separate the digits in 370 - 3,7,0.
- Calculate the sum => (3)3+(7)3+(0)3 = 370.
- Sum obtained in step-3 is 370 which is equal to the given number. Hence, 370 is an Armstrong number.
Java code to check for an Armstrong number
package com.javabrahman.generaljava;
import java.util.Scanner;
public class ArmstrongNumberCheck {
public static void main(String args[]) {
//Input number to be tested
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
//Convert number in String format to Long format
long inputNo = Long.parseLong(str);
//Call to method to count number of digits
int digitCount = countNoOfDigits(inputNo);
//call to method to calculate sum
long sumOfDigits = calculateSum(inputNo, digitCount);
if (inputNo == sumOfDigits) {
System.out.println(inputNo + " is an Armstrong Number");
} else {
System.out.println(inputNo + " is NOT an Armstrong Number");
}
}
/**
* Method counts the number of digits of inputNo
* @param inputNo
* @return count of digits
*/
public static int countNoOfDigits(long inputNo) {
int count = 1;
while ((inputNo = inputNo / 10) > 0) {
count++;
}
return count;
}
/**
* Method calculates the sum of digits
* with each raised to the power of count of digits
* @param inputNo
* @param digitCount
* @return sum of digits raised to power count of digits
*/
public static long calculateSum(long inputNo, int digitCount) {
long sum = 0;
long num = inputNo;
while (num > 0) {
long remainder = num % 10;
sum += Math.pow(remainder, digitCount);
num = num / 10;
}
return sum;
}
}
370 370 is an Armstrong Number 400 400 is NOT an Armstrong Number
ArmstrongNumberCheck
uses aScanner
instance to fetch the given number entered by the user to be checked for an Armstrong number.- The input number is converted from a
String
value to along
value, namedinputNo
, usingLong.parseLong()
method. inputNo
is sent as a parameter to the methodcountNoOfDigits()
which returns the total number of digits ininputNo
and stores it in the variabledigitCount
.countNoOfDigits()
works by dividing theinputNo
by10
in a loop. For every iteration the rightmost digit is removed (as remainders are not retained in a long value). So, for n iterations, n rightmost digits are removed, and once the number is 0, there is no digit remaining. At this point n will be the count of number of digits in the number.inputNo
anddigitCount
are sent as parameters tocalculateSum()
method.calculateSum()
method separates the digits ofinputNo
by using %(remainder) by 10. In each loop, the remainder on dividing by 10 is the rightmost digit of the number.Math.pow()
method is used to calculate the sum of digits raised to the total count of digits.- The sum obtained is returned to the
main()
method. This sum value is compared to theinputNo
to determine whether the given number is an Armstrong number or not.