cpp 专题
专题目录
您的位置:cpp > cpp 专题 > C++阿姆斯壮数字
C++阿姆斯壮数字
作者:--    发布时间:2019-11-20

在编写c++程序以检查数字是否为阿姆斯壮数字之前,先要来了解一下阿姆斯壮数字是什么。

阿姆斯壮数字是等于其数字的立方之和的数字。 例如:0,1,153,370``,371407是阿姆斯壮数字。

下面说明为什么371是阿姆斯壮数字。

371 = (3*3*3)+(7*7*7)+(1*1*1)    
这里:    
(3*3*3)=27    
(7*7*7)=343    
(1*1*1)=1    
所以:    
27+343+1=371

让我们来看看如何使用c++程序来判断阿姆斯壮数字

#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.

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册