Here I'm comparing ArmstrongNo & out were both have same values(371), but is printing the wrong statement.
public class ArmstrongNumber {
static int ArmstrongNo = 371;
static int sum = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
ArmstrongNumber am = new ArmstrongNumber();
int out = am.Armstrong();
System.out.println(out);
if (ArmstrongNo == out)
System.out.println("It is an Armstrong number");
else
System.out.println("Not an Armstrong number");
}
public int Armstrong() {
int length = String.valueOf(ArmstrongNo).length();// To find the length of integer
for (int x = 0; x < length; x++) {
int i = ArmstrongNo % 10;
int cube = i * i * i;
sum += cube;
ArmstrongNo = ArmstrongNo / 10;
}
return sum;
}
}
OUTPUT:
371
Not an Armstrong number