Armstrong Number in C++

Armstrong number is a number which is equal to sum of digits raise to the power total number of digits in the number. For example, 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371.

Source code :


#include<iostream>

using namespace std;

int main()
{
   int sum = 0, t, r;
   
   int num = 371; //Change the number here

   t = num;
   while( t != 0 )
   {
      r = t%10;
      sum = sum + r*r*r;
      t = t/10;
   }

   if ( num == sum )
        cout<< num <<" is an armstrong number";
   else
        cout<< num <<" is not an armstrong number";

   return 0;
}


Output :

371 is armstrong number

Notes :

371 = 333 + 777 + 111
153 = 111 + 555 + 333
371 and 153 are Armstrong numbers


Output :


                

Comments :