0

Tried to write a logic to check whether the given number is armstrong number or not. Here I need a help. Though the values are same it always prints false and as per the logic it says the numbers are not equal.

package src;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ArmStrongTest {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(isArmStrongNumber(153));// prints false;

    }

    private static boolean isArmStrongNumber(int nos) {

        List<Integer> digits = new ArrayList<Integer>();
        int itrNumbers = 0;

        while (nos > 0) {
            digits.add(nos % 10);
            nos /= 10;
        }

        Collections.reverse(digits);

        for (int i : digits) {
            itrNumbers += i * i * i;

        }

        System.out.println(java.util.Objects.equals(Integer.valueOf(itrNumbers), Integer.valueOf(nos)));// prints false;

        if (itrNumbers == nos ) { // both values are 153 but it says not equals

            return true;

        } else {

            return false;

        }

    }

}

How to solve this problem? Anything I missed in the code ? I am not able to figure out. I am getting false printed all the time. In such cases how to solve this problem?

phuclv
  • 37,963
  • 15
  • 156
  • 475
JAVA_CAT
  • 737
  • 3
  • 13
  • 33

0 Answers0