用c语言写一个判断水仙花数的代码
时间: 2023-04-10 16:02:51 浏览: 160
以下是判断水仙花数的C语言代码:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("请输入一个三位数:");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num) {
printf("%d 是水仙花数", num);
} else {
printf("%d 不是水仙花数", num);
}
return 0;
}
相关问题
用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!
c语言判断水仙花数代码
下面是c语言判断水仙花数的代码:
```c
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("请输入一个正整数: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// 计算每一位数字的立方和
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
// 判断是否为水仙花数
if (result == num) {
printf("%d 是水仙花数", num);
} else {
printf("%d 不是水仙花数", num);
}
return 0;
}
```
该程序通过循环计算输入的数字的每一位数字的立方和,然后判断是否与原数字相等,如果相等则是水仙花数,否则不是。
阅读全文