A number is said to be a Armstrong number if it is equal to the sum of the cube of its digits.
Some examples of Armstrong numbers are:
- 153
- 1
- 370
- 407
Explanation of 1st example:
153 = 1³ + 5³ + 3³
NOTE : 0 is a Armstrong number because 0³ = 0.
Now here’s we come to the coding part. I am writing the function to check whether a given number is a Armstrong number or not.
In Python:
def Armstrong_number(x):
res = 0
copy = x
while x != 0:
res += (x % 10) ** 3
x = x // 10
return res == copy
In C:
void Armstrong_numbers(int x)
{
int res = 0, copy = x;
while(x != 0)
{
res = res + ((x % 10)*(x % 10)*(x % 10));
x = x / 10;
}
if (res == copy)
{
printf("Armstrong number");
}
else
{
printf("Not a Armstrong number");
}
}
Happy Coding!