C++ 阿姆斯朗数字
在编写C++程序以检查该数字是否为Armstrong之前,让我们了解什么是阿姆斯壮编号。
阿姆斯壮数字是一个等于其数字的立方之和的数字。例如0、1、153、370、371和407是阿姆斯壮数字。
让我们尝试理解为什么 371 是阿姆斯壮数字。
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++程序来检查Armstrong编号。
#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cout<<"Enter the Number= ";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
cout<<"Armstrong Number."<<endl;
else
cout<<"Not Armstrong Number."<<endl;
return 0;
}
输出:
Enter the Number= 371
Armstrong Number.
Enter the Number= 342
Not Armstrong Number.