Armstrong number in C++ by R4R Team

Armstrong number:
-A number which is equal to the sum of the power of digit by its length is called as Armstrong number.

Example-
1,2,3,4,5,6,7,8,9
these all are Armstrong number
370,371 are Armstrong number.
How ?
370=3^3+7^3+0^3
-Here '^' shows the power operator

program:

#include < iostream>
#include
using namespace std;
class r4r
{
public:
int Armstrong(int n)
{
int length=0,sum=0,n1;
n1=n;
while(n)
{
length++;
n=n/10;
}
n=n1;
while(n)
{
sum=sum+pow(n%10,length);
n=n/10;
}
if(sum==n1)
cout<<"Number is Armstrong number"<< endl;
else
cout<<"Number is not armstrong";
}
};
int main()
{
int n;
r4r obj;
cout<<"Enter any number"<< endl;
cin>>n;
obj.Armstrong(n);
return 0;
}


output-

Enter any number
370
Number is armstrong number

Enter any number
89
Number is not armstrong




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!