Veslor Veslor - 1 year ago 107
Java Question

Armstrong Number java

I've got a problem with some simple code. I haven't seen where is the problem in my code. It returns false when it should return true, since 153 is an Armstrong Number.

Following is my code:

public class Armstrong {

static double nArms, unidad, decena, centena, aux;



Armstrong(double nArms){
this.nArms = nArms;

public boolean esArmstrong(double nArms){

aux = nArms % 100;
centena = nArms / 100;
decena = aux / 10;
unidad = aux % 10;

this.nArms = Math.pow(unidad, 3) + Math.pow(decena, 3) +Math.pow(centena, 3);

if(this.nArms == nArms){
return true;
return false;

public static void main(String[] args) {

Armstrong arms = new Armstrong();




Answer Source

You are using double when you intend to do integer arithmetic. For example, when you write

centena = nArms / 100;

you are doing floating point division, (and centena is assigned the value 1.53) but you want to perform integer division. Use int, long (or BigInteger) instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download