on Leave a Comment

C++ Program to Check Number is Armstrong or Not

To understand C++ program to check Armstrong number, you must know - C++ looping

If the sum of cube of an positive integer is equal to that positive integer, then positive integer is called an Armstrong number. 

For example:

407=4*4*4+0*0*0+7*7*7 //Armstrong number
123 is not equal to 1*1*1+2*2*2+3*3*3 //Not an Armstrong number

C++ Program to Check Number is Armstrong or Not

#include<iostream>
using namespace std;
int main()
{
    int num,n,d,s=0;
    cout<<"Enter a number : ";
    cin>>num;
    n=num;
    while(num!=0)
    {
        d=num%10;
        s=s+d*d*d;
        num/=10;
    }
    if(s==n)
        cout<<endl<<n<<" is an Armstrong number";
    else
        cout<<endl<<n<<" is not an Armstrong number";
    return 0;
}

OUTPUT 1:

Enter a number : 407

407 is an Armstrong number

OUTPUT 2:

Enter a number : 123

123 is not an Armstrong number

0 comments:

Post a Comment