Sunday 25 September 2011

To find the given number is an armstrong number or not ?

Armstrong Number


Also known as narcissistic numbers, Armstrong numbers are the sum of their own digits to the power of the number of digits. As that is a slightly brief wording, let me give an example:
153 = 1³ + 5³ + 3³


Here you can see this program-

//To find the given number is an armstrong number or not ?


# include <iostream.h>
# include <conio.h>
# include <math.h>


void main ()
{
clrscr();
  int a,b,sum=0;
  long int n;
  cout<<"Enter the No. : ";
  cin>>n;
   b = n;
  while(n > 0) //counts the digits
   {
    a = n%10;
    sum = sum + (a*a*a);
    n = n/10;
}
  if(sum == b)
  {
    cout<<"IT IS AN ARMSTRONG NUMBER...";
  getch();
  }
  else
  {
    cout<<"IT IS NOT AN ARMSTRONG NUMBER...";
    getch();
  }
}

No comments:

Post a Comment