Regarding armstrong no

sir my code failed three test cases ,please tell me the mistakes:
#include
#include<math.h>
using namespace std;
int main()
{
int n,rem=0,cnt=0,arm_no=0;
cin>>n;
while(n>0)
{
rem=n%10;
if(rem>0)
{
cnt++;
}
n=n/10;

}
while(n>0)
{
	rem=n%10;
	arm_no=arm_no+pow(rem,cnt);
	n=n/10;
}
if(arm_no==n)
{
	cout<<"true"<<endl;
}
else
{
	cout<<"false"<<endl;
}
return 0;

}

Hi @Megha2468
In your code you are using n in both the while loop but see after first while loop n becomes zero and that is why second while loop does not run. And also in the first loop you are incrementing cnt only if res > 0 but in this you are not counting the length of input correctly because for input 100 you will get cnt as 1 but it should be 3.

Try to correct these mistakes you will get all test cases passed. If still unable to get it here is your corrected code, have a look at it :

sir why long long is used instead of int

@Megha2468
You can use int also that wont make a difference in this question as n<1000000000

siir now also the code is not passing all the test cases!!!

@Megha2468
The code i have send you will be passing all the test cases. Kindly send me your updated code ide so that i can have a look.

#include
#include<math.h>
using namespace std;
int main()
{
int n,rem=0,cnt=0,arm_no=0;
cin>>n;
int x=n;
while(n>0)
{
rem=rem%10;
cnt++;
n=n/10;
}
x=n;
while(n>0)
{
rem=rem%10;
arm_no=arm_no+pow(rem,cnt);
n=n/10;
}
x=n;
if(arm_no==n)
{
cout<<“true”<<endl;
}
else
{
cout<<“false”<<endl;
}
return 0;
}

@Megha2468
At line 15 and 22 it should be n=x as you have to restore value of n back to n. And also you are doing rem=rem%10 but it should be rem=n%10 in both the while loops. Then you code will pass all the test cases.

sir after correction also not passing all test case
passed 3
failed 4
initially it was passing 4 and falling 3

@Megha2468
You are doing rem=rem%10 but it should be rem=n%10 in both the while loops. Then you code will pass all the test cases.