在 C 中检查数字是否为 Armstrong Number
Checking whether number is Armstrong Number or not in C
阿姆斯特朗数 是一个数,它等于其数字的立方之和。例如 0、1、153、370、371 和 407 是阿姆斯壮编号。
我是这样试过这个程序的-
// Program to check whether number is Armstrong number or not
#include<stdio.h>
int main()
{
int a,r,sum=0,temp;
//accepting input from user
printf("Enter a number to check whether it is Armstrong number: ");
scanf("%d", &a);
//condition for checking if sum of individual numbers cube is equal to number
do
{
r = a % 10;
sum += r*r*r;
a = a/10;
} while (a>0);
//printing final result.
if(temp == sum)
{printf("It is Armstrong Number.");}
else
{
printf("It is not Armstrong Number.");
}
return 0;
}
在这里,我得到的结果总是不是阿姆斯特朗号码,所以我上网查了一下,他们用一个临时变量来存储输入的号码。为什么这是必要的?
添加临时变量 amke 代码是如何工作的?其他错误是什么?
这是有效的代码:-
// Program to check whether number is Armstrong number or not
#include<stdio.h>
int main()
{
int a,r,sum=0,temp;
//accepting input from user
printf("Enter a number to check whether it is Armstrong number: ");
scanf("%d", &a);
temp = a;
//condition for checking if sum of individual numbers cube is equal to number
do
{
r = a % 10;
sum += r*r*r;
a = a/10;
} while (a>0);
//printing final result.
if(temp == sum)
{printf("It is Armstrong Number.");}
else
{
printf("It is not Armstrong Number.");
}
return 0;
}
-
int a,r,sum=0,temp;
temp
没有在第一个代码中初始化。
-
they used a temp variable to store the input number. Why is that neccessary?
原因:执行do-while循环后,a
的值变为0。因此,您没有原始数字可以与 sum
变量进行比较。因此,我们复制原始数字 a
.
您在条件 temp == sum
中使用了 temp
,但在此之前您没有为 temp
分配任何内容。这里temp
的值是不确定的,因为它是一个未初始化的非静态变量(具有自动存储期)。使用这种不确定的值会调用 未定义的行为。
其他错误是缩进不一致,scanf()
的结果没有被检查。乘法和加法中没有检查以避免溢出也可以算作错误。
阿姆斯特朗数 是一个数,它等于其数字的立方之和。例如 0、1、153、370、371 和 407 是阿姆斯壮编号。
我是这样试过这个程序的-
// Program to check whether number is Armstrong number or not
#include<stdio.h>
int main()
{
int a,r,sum=0,temp;
//accepting input from user
printf("Enter a number to check whether it is Armstrong number: ");
scanf("%d", &a);
//condition for checking if sum of individual numbers cube is equal to number
do
{
r = a % 10;
sum += r*r*r;
a = a/10;
} while (a>0);
//printing final result.
if(temp == sum)
{printf("It is Armstrong Number.");}
else
{
printf("It is not Armstrong Number.");
}
return 0;
}
在这里,我得到的结果总是不是阿姆斯特朗号码,所以我上网查了一下,他们用一个临时变量来存储输入的号码。为什么这是必要的? 添加临时变量 amke 代码是如何工作的?其他错误是什么? 这是有效的代码:-
// Program to check whether number is Armstrong number or not
#include<stdio.h>
int main()
{
int a,r,sum=0,temp;
//accepting input from user
printf("Enter a number to check whether it is Armstrong number: ");
scanf("%d", &a);
temp = a;
//condition for checking if sum of individual numbers cube is equal to number
do
{
r = a % 10;
sum += r*r*r;
a = a/10;
} while (a>0);
//printing final result.
if(temp == sum)
{printf("It is Armstrong Number.");}
else
{
printf("It is not Armstrong Number.");
}
return 0;
}
-
int a,r,sum=0,temp;
temp
没有在第一个代码中初始化。
-
they used a temp variable to store the input number. Why is that neccessary?
原因:执行do-while循环后,a
的值变为0。因此,您没有原始数字可以与 sum
变量进行比较。因此,我们复制原始数字 a
.
您在条件 temp == sum
中使用了 temp
,但在此之前您没有为 temp
分配任何内容。这里temp
的值是不确定的,因为它是一个未初始化的非静态变量(具有自动存储期)。使用这种不确定的值会调用 未定义的行为。
其他错误是缩进不一致,scanf()
的结果没有被检查。乘法和加法中没有检查以避免溢出也可以算作错误。