Armstrong Number

What is wrong in this code.
if(sum==n)
return true;

You have the check if(sum == n) out side the while loop, rest everything is fine with your code.
If you check inside the while loop, then your are checking for every digit instead of the final number.

But If I check the condition outside the loop the sum variable will no longer be having the value as the memory allocated for sum is destroyed when the loop ends.

You have declared the sum variable outside the loop, so the memory allocated to the sum variable will not be destroyed.

#include
using namespace std;

bool armstrong(int n)
{
int no,d,cube,sum=0;
no=n;
while(no!=0)
{
d=no%10;
cube=ddd;
sum+=cube;
no/=10;

}

if(sum==n)
return true;

else
return false;

}

int main()
{

int n;
cin>>n;

cout<<armstrong(n);

return 0;
}

Now also it says the code is wrong

True is returned as 1 and false is returned as 0 by the function. I tweaked your code a little so that it gives the correct output, other than that rest of your code and logic is fine.
Here :

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.