Armstrong Number

#include<bits/stdc++.h>
using namespace std;
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
if (y%2 == 0)
return power(x, y/2)power(x, y/2);
return x
power(x, y/2)*power(x, y/2);
}

int order(int x)
{
int n = 0;
while (x)
{
n++;
x = x/10;
}
return n;
}
bool isArmstrong(int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp)
{
int r = temp%10;
sum += power(r, n);
temp = temp/10;
}

return (sum == x); 

}
int main()
{
int x;
cin>>x;
cout << isArmstrong(x) << endl;
return 0;
}

This code give wrong output for the test cases.

You don’t have to do all this. This question has a very simple solution. No need to make a power function and all that. Just make a single function which checks if the sum of each digit, raised to the power 3, is equal to the number itself. If it is, then return true. Else, return false.

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.