该程序没有显示153作为Armstrong编号,而其他编号的输出正确。就像我检查407一样,它给出了正确的答案,但是当我检查153时,它显示的不是阿姆斯特朗数字。针对Armstrong编号给出错误输出的C程序
#include <stdio.h>
#include <math.h>
int main() {
int no, copy, re, n = 0, ans = 0;
printf("\n\tEnter a new number: ");
scanf("%d", &no);
copy = no;
while (copy != 0) {
copy = copy/10;
n++;
}
copy = no;
while (copy != 0) {
re = copy % 10;
ans = ans + pow(re, n);
copy = copy/10;
}
if (ans == no) {
printf("\n\t %d is an Armstrong number", no);
} else {
printf("\n\t %d is not an Armstrong number", no);
}
getch();
return 0;
}
你是否在调试器中逐行扫描你的代码?这是调试这些问题的正确方法,只有在尽力调试自己之后才能找到其他人的帮助。 – kaylum
你的代码显示了我给Armstrong的任何号码。 – Marievi
1)缩进代码2)正确命名你的变量。 –