用c语言写代码判断水仙花数
时间: 2024-01-29 14:04:54 浏览: 69
c语言代码水仙花数
Sure, I can help with that! Here is the C code to check whether a number is an Armstrong number:
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// count the number of digits
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
originalNum = num;
// calculate the result
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// check whether 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;
}
Hope that helps!
阅读全文