public static bool IsArmstrongNumber(int n)
{
int r, cube, sum = 0, input = n;
if (n < 10)
return true;
while (n != 0)
{
r = n % 10;
cube = r * r * r;
sum = sum + cube;
n = n / 10;
}
return sum == input ? true : false;
}
How it works:
Calculating an Armstrong number relies on the same basic Divisibility rule of 10, on which the logic to count the number of digits can also be made.
For each iteration till the number is made 0, we strip the number of its last digit by modulus dividing with 10 and then cube the remainder and add it up to a total sum. Finally once the number is completely dealt, we check if the total sum is equal to the input number or not. If equal, it is an Armstrong number else not.