C语言阿姆斯特朗号
在编写c程序以检查该数字是否为Armstrong之前,让我们了解什么是阿姆斯特朗号。
阿姆斯壮数字是一个等于其数字的立方之和的数字。例如0、1、153、370、371和407是阿姆斯壮数字。
让我们尝试理解为什么 153 是阿姆斯壮数字。
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
让我们尝试理解为什么 371 是Armstrong号码。
371 = (3*3*3)+(7*7*7)+(1*1*1)
where:
(3*3*3)=27
(7*7*7)=343
(1*1*1)=1
So:
27+343+1=371
让我们看一下用c程序检查C中的Armstrong编号。
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
输出:
enter the number=153
armstrong number
enter the number=5
not armstrong number