我试图从输入中查找是否是“阿姆斯特朗数”。这是我的代码。也许我有另一个箭头,但我不能用 int * 变量除(/)或乘(*)。这是为什么?
另外,查找“阿姆斯特朗号码”并为用户提供信息的最佳方式是什么?
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
int n = 0, c = 0, result = 0;
printf( "Enter a number: " );
scanf("%d", &n);
int digit = log10(n)+1;
int ntemp1 = n;
while (ntemp1 != 0){
ntemp1 /= 10;
c++;
}
ntemp1 = n;
for (int i = 0; i < c; i++){
result += pow(ntemp1 % 10, c);
ntemp1 /= 10;
}
if (result == n)
printf("\nIt is an Armstrong Number!\n");
else
printf("\nIt is not an Armstrong Number!\n");
}
// Okey now: i understand important thing about pointer. That's i'll remember always. I did like this now. And it's look okey and find "is Armstrong Number or not".
scanf("%d", s);
未定义的行为,因为
s
为 NULL。您需要将指针传递到某个位置 scanf
可以存储 int
。
scopy /= 10;
划分指针没有任何意义。除法需要一个数字。
scopy[j]
如果我们忽略除法来到达此位置,则您将 NULL 分配给
scopy
,并且解除对 NULL 指针的引用是未定义的行为。
至于修复,我不知道你想做什么,但你的代码应该以
开头unsigned n = 0;
printf( "Enter a number: " );
if ( scanf( "%u", &n ) != 1 ) {
fprintf( stderr, "Invalid input.\n" );
exit( 1 );
}