c - 在 c 中生成阿姆斯特朗数到第 n 个数不起作用

标签 c math

我正在尝试生成第 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/

相关文章:

c - 简单的 libnotify "hello world"程序无法在 Ubuntu 20.04 上编译并出现链接器错误

c# - 为什么 vector.Length 比 Math.sqrt() 快?

c - Linux内核模块复制进程的.text段

c++ - 将字符串中表示的大数除以 3

algorithm - 解决有序列表中的模糊类别

math - 如何计算秤的结果?

javascript - 计算地理位置的距离

c - 扫描名称和奇怪的字符串: Files

c++ - C++ try/catch 与 C setjmp/longjmp 有何不同?

c - 跳过 IEEE 754 中的 "test for zero"检查