16.c++ program to check Armstrong number or not


FIRST METHOD: 

// C++ program to check Armstrong number or not of 3 digits

INPUT:

#include<iostream>

using namespace std;

int main()

{

    int n,r,sum=0,z;

    cout<<"Enter the number : ";

    cin>>n;

    z=n;

    while(n>0)

    {

        r=n%10;

        sum = sum+(r*r*r);

        n=n/10;

    }

    if(z==sum)

        cout<<"\n\t"<<z<<" is an Armstrong number "<<endl;

    else

        cout<<"\n\t"<<z<<" is not an Armstrong number "<<endl;

    return 0;



SECOND METHOD:

// C++ program to check Armstrong number of N digits

INPUT:

#include<iostream>

#include<cmath>

using namespace std;

int main()

{

    int num,a,r,n=0,res=0,pwr;

    cout<<"Enter an integer : ";

    cin>>num;

    a=num;

    while(a!=0)

    {

        a=a/10;

        n++;

    }

    a=num;

    while(a!=0)

    {

        r=a%10;

        pwr = round(pow(r,n));

        res = res + pwr;

        a = a/10;


    }

    if(res == num)

        cout<<"\n\t"<<num<<" is an Armstrong number "<<endl;

    else

        cout<<"\n\t"<<num<<" is not an Armstrong number "<<endl;

    return 0;

}

}


OUTPUT:



Comments