Programming Issue

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);

		int num = sc.nextInt();
		int aman = num;
		int ans = 0;
		while (num != 0) {
			int rem = num % 10;
			ans = ans + (rem * rem * rem);
			num = num / 10;
		}
		if (ans == aman) {
			System.out.println("true");
		} else
			System.out.println("false");

}

Here is my Armstrong number.Out of 7 I am able to pass only 5 test cases.please clarify my doubt what changes my code required.

@amangaur078,

Algo:

  • Take input of number.
  • Calculate the number of digits of the number in a variable say , cnt.
  • Extract digit one by one from the number(using % 10).
  • Raise it to the power of number of digits and add it to sum.
  • After the loop check if the sum is equal to the original number.
    1. If yes, print true
    2. otherwise, print false.

You need to raise it to the power of total digits instead of just 3.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.