Armstrong number

#include
#include<math.h>
#include
using namespace std;
int main()
{
int n,m,sum,rem,a,b;
cout<<“Enter the number”;
cin>>n;
if(n<0)
{
n=abs(n);
a=abs(n);
b=abs(n);
}
else{
a=n;
b=n;
}

m=0;
sum=0;
while(n!=0)
{
rem=n%10;
m++;
n=n/10;
}
cout<<m<<"\n";
while(a!=0)
{
rem=a%10;
sum+=pow(rem,m);
a=a/10;
}
cout<<sum<<"\n";
if(sum==b)
{
cout<<“Yes”;
}
else
{
cout<<“No”;
}
return 0;;
}

The above code is running perfectly in codeblocks but is showing a error while submitting

hello @mahi.1305
check output format of this problem.
and then print only those things that are mentioned there.

also no need of such conditions. given input will always be valid

Sir,
i added these conditions for the negative number.
because during submission of the code an error message is being displayed asking to apply a code for negative numbers too.

image

as per constraints n will be always positive so no need to handle negative case.

ignore this message.

remember one thing , whatever is mentioned in constraint section of any will always be followed on given input, so there is no need to add additional code for handling constraint voilation

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.

1 Like