Check given number is armstrong or not

import java.util.*;
public class Main {

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

    int  number, temp, total = 0;

    number = num;
    while (number!=0)
    {
		
        temp = number % 10;
        total = total + temp*temp*temp;
        number /= 10;
    }

    if(total == num)
        System.out.println("true");
    else
        System.out.println("false");
}

}
where i went wrong still some testcases are not excecuted

@karthik1989photos,

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 did that only know sir

@karthik1989photos,
Can you please rephrase?

import java.util.*;
public class Main {

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

    int  number, temp, total = 0,count=0;

    number = num;
    while (number > 0)
    {
		
        temp = number % 10;
        total = total + temp*temp*temp;
        number /= 10;
		count++;
    }

    if(total == num)
        System.out.println("true");
    else
        System.out.println("false");
}

}

i rephrased the code y initialising count still testcases 0 and1 went wrong

@karthik1989photos,
You have to count the number of digits first. Then raise every digit to the power of total digits in the number.

See the example:
1634 is an Armstrong number as 1634 = 1^4 + 6^4 + 3^4 + 4^4
Here 4 is the total digits.

sir i am unable to solve this can you provide the code

@karthik1989photos,

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.