I Found this solution ( I am still learning C) what can be a better solution to this?
I Found this solution ( I am still learning C) what can be a better solution to this?
An Armstrong number is a n digit number, which is equal to the sum of the nth powers of its digits. For example: 153 is an Armstrong number because the sum of it's digits raised to the n-th power is equal to the number itself --> 1^3 + 5^3 + 3^3 = 153.
int getNumberOfDigits(int number) {
int counter = 0;
while(number != 0)
{
counter++;
number = number/10;
}
return counter;
}
void isArmstrong(int number)
{
int digitsCount = getNumberOfDigits(number);
int sum = 0, old_number = number; // saving the number in a variable so we can compare our answer to it
while(number != 0) {
sum += pow(number%10,digitsCount); // number%10 returns the last digit in the number
number = number/10;
}
if(sum == old_number)
printf("%d is an armstrong number",old_number);
else
printf("%d is not an armstrong number",old_number);
}