C++ Program to check whether a number is Armstrong number or not [DEVCPP/GCC]


ARMSTRONG NUMBER

A number of m digits is said to be an Armstrong number, if the sum of its each digit raise to the power m results in the original number.

For example,
5^1=5
(1^3) + (5^3) + (3^3) = 153
(8^4) + (2^4) + (0^4) + (8^4) = 8208

PROGRAM

// Program to determine whether given number is Armstrong Number or not

#include <iostream>

using namespace std;

int main()
{
int num ,n, sum=0, temp,count=0, power;

cout<<"ENTER NUMBER: ";
cin>>n;

num = n;

//To count number of digits

while(num>0)
{
count++;
num=num /10;
}

         //Reassigning n to num
 
num = n;


while(num>0)
{
temp = num % 10;    //Obtain the last digit of num

// Calculate power of obtained digit(temp)

power=1;
for(int i=0;i<count;i++)
{
power=power*temp;
}

sum = sum + power; //Rebuilding number
num = num /10; //To remove the last digit
}

if(sum==n)
{
cout<<n<<" IS AN ARMSTRONG NUMBER";
}
else
{
cout<<n<<" IS NOT AN ARMSTRONG NUMBER";
}

return 0;
}

OUTPUT

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

Share this

Related Posts

FIND US ON FACEBOOK!