C++ Program to check whether a number is Armstrong or not?

What is an Armstrong number?

A three digit number is an Armstrong number if sum of cube of its digits is equal to itself.

Program:

#include<iostream>
using namespace std;
int armstrong(int x);
int cube(int c);
int main()
{
int num;
cout<<"Enter a three digit Number="<<endl;
cin>>num;

    if(armstrong(num)==num)
    {
    cout<<"Number is Armstrong";
    }
    else
    {
    cout<<"Number is not Armstrong";
    }
return 0;
}
//Armstrong Function
int armstrong(int x)
{
int a,sum=0,result;
/* a=x%10;
b=x/10;
c=b%10;
d=b/10;
int result;
result=cube(a)+cube(c)+cube(d);
return result;*/
do
{
a=x%10;
sum=sum+cube(a);
x=x/10;
}while(x!=0);
result=sum;
return result;
}
//Cube Function
int cube(int c)
{
int r;
r=c*c*c;
return r;
}

Output:



Comments

Popular posts from this blog

C++ Program to take random numbers from user and calculate their sum