Unable to pass two test cases with this code

#include
using namespace std;

int main(){

int n, armNo = 0, m;
int k;
cin >> m;
n = m;


while(n != 0){
     k = n%10;
    armNo = armNo + k*k*k;
    n = n/10;
}
if(m == armNo){
    cout << "true" << endl;
}
else{
    cout << "false" << endl;
}


return 0;

}

Your code will give correct answer only for 3 digit numbers.
Say you are given a 4 digit number 1634
1634= 1^4 + 6^4 + 3^4 + 4^4 (So 1634 is an armstrong number)
Similarly:
153= 1^3 + 5^3 + 3^3 (So 153 is an armstrong number)
So by these examples you will get what an armstrong number is.
So to check if a number is armstrong number you just have to count the number of digits and extract each digit. Maintain a sum of (digit)^ noofdigits. If at the end of the loop, this sum==the given number then the number is armstrong else it is not armstrong.

1 Like

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.