c语言用函数求水仙花数
时间: 2024-03-20 10:35:33 浏览: 69
C语言求水仙花数
以下是使用C语言编写的函数来求水仙花数的示例代码:
```c
#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int originalNum, remainder, n = 0, result = 0;
originalNum = num;
// 计算数字的位数
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// 计算每位数字的n次幂并求和
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// 判断是否为水仙花数
if (result == num)
return 1;
else
return 0;
}
int main() {
int start, end;
printf("请输入范围: ");
scanf("%d %d", &start, &end);
printf("水仙花数: ");
for (int i = start; i <= end; i++) {
if (isArmstrong(i))
printf("%d ", i);
}
return 0;
}
```
阅读全文