How are narcissistic numbers represented in the C language?
In the C programming language, you can use the following code to represent narcissistic numbers:
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, result = 0, n = 0;
printf("请输入一个三位数:");
scanf("%d", &num);
originalNum = num;
// 计算位数
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// 计算水仙花数
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
printf("%d 是一个水仙花数", num);
else
printf("%d 不是一个水仙花数", num);
return 0;
}
In the above code, by entering a three-digit number, first calculate the number of digits n in that number, then raise each digit on that number to the power of n, and finally add all these results to get a result. If the result is equal to the original three-digit number num, then the number is an Armstrong number; otherwise, it is not an Armstrong number.