Problem no 4: Armstrong number.
QUESTION:
For a given 3 digit number, find whether it is armstrong number or not. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Return "Yes" if it is a armstrong number else return "No".
NOTE: 371 is an Armstrong number since 33 + 73 + 13 = 371
ANSWER:
string armstrongNumber(int n){
int x=n;
int result=0;
while(n>0)
{
int rem =n%10;
result=result+pow(rem,3);
n=n/10;
}
if(x==result)
{
return "Yes";
}
else
{
return "No";
}
}
Comments
Post a Comment