📜  C程序检查阿姆斯壮编号

📅  最后修改于: 2020-10-04 11:20:04             🧑  作者: Mango

###### 在此示例中，您将学习检查用户输入的整数是否是Armstrong数字。

``````abcd... = an + bn + cn + dn +
``````

``````153 = 1*1*1 + 5*5*5 + 3*3*3
``````

##### 检查阿姆斯壮三位数
``````#include
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the orignal number
originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}
``````

``````Enter a three-digit integer: 371
371 is an Armstrong number.
``````

##### 检查阿姆斯壮n位数字
``````#include
#include

int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;

printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;

// store the number of digits of num in n
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}

for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;

// store the sum of the power of individual digits in result
result += pow(remainder, n);
}

// if num is equal to result, the number is an Armstrong number
if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}``````

``````Enter an integer: 1634
1634 is an Armstrong number.
``````