我正在尝试生成第 n 个数字的阿姆斯特朗数字。所以,我为此编写了一个代码,但它不起作用。每次我输入像 999 或 10000 这样的数字,它只返回 0....任何人都可以请帮我找出这段代码有什么问题:
#include <stdio.h>
#include <math.h>
int main()
{
double remainder,n=0;
int number,sum = 0,q,x,count;
printf("Enter an integer upto which you want to find armstrong numbers:");
scanf("%d",&number);
printf("\nFollowing armstrong numbers are found from 1 to %d\n",number);
for( count = 1 ; count <= number ; count++ )
{
q = count;
x = count;
for(;x != 0;)
{
x =x / 10;
n = n + 1;
}
while(q != 0)
{
remainder = q % 10;
sum = sum +(pow(remainder,n));
q = q / 10;
}
if ( count == sum ){
printf("%d\n", count);
}
sum = 0;
}
return 0;
}
最佳答案
您必须在 count
循环内将 n
初始化为零。并注意对 pow
结果进行适当舍入,例如向其中添加 0.5
。
关于c - 在 c 中生成阿姆斯特朗数到第 n 个数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200170/