At 10/22/2009 08:02 AM, you wrote:

># include <iostream.h>
># include <conio.h>
># include <math.h>
>  void main ()
>{ clrscr();
>  int a,b=0,sum=0;
>  long int n;
>  cout<<"ENter the NO. : ";
>  cin>>n;
>  for(;n>0;)
>//counts the digits
>  { a=n%10;
>    n=n/10;
>    b++;
>  }
>  for(;n>0;)
>  { a=n%10;
>    sum=sum+pow(a,b);
>    n=n/10;
>  }
>   if(sum==n)
>  { cout<<"IT IS AN ARMSTRONG NUMBER...";
>   getch();
>  }
>   else
>  { cout<<"IT IS NOT AN ARMSTRONG NUMBER...";
>    getch();
>  }
>}
>
>
>-- in this code every time the ans is "IT IS AN ARM STRONG NUMBER....' why
>is this so i m having problem in making this program with the help of class
>can i get some help
>shruti dixit

You have several problems. The biggest problem is probably that you 
haven't defined you algorithm correctly. I looked up Armstrong 
numbers (here: 
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html) 
and it says "An Armstrong number is a number such that the sum of its 
digits raised to the third power is equal to the number itself".

You are raising the digits to the number of digits in the number, not 
by 3. (BTW: I don't believe 153 is a valid Armstrong number as stated 
on the web site).

I put the computation into a function -- check below.

~Rick

// An Armstrong number is a number such that the sum of its digits
// raised to the third power is equal to the number itself.

// For example, 371 is an Armstrong number, since 3**3 + 7**3 + 1**3 = 371.

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

# include <math.h>


bool isArmstrong(long int number)
{
long int    digit;
long int    value;
long int    sum = 0;

// Compute the sum
     value = number; // Save the original value
     while (value > 0)
     {
         digit = (value % 10);
         sum += (long int)pow((double)digit, 3);
         value /= 10;
     }

     return (sum == number);
}


int main(int argc, char *argv[])
{
long int    number;

for (number = 0; number < 1000; ++number)
{
//    cout << "Enter the number: ";
//    cin >> number;


if (isArmstrong(number))
{
     cout << setw(5) << number << " IS AN ARMSTRONG NUMBER.\n";
}
else
{
//    cout << setw(5) << number << " IS NOT AN ARMSTRONG NUMBER.\n";
}
}
     return EXIT_SUCCESS;
}


[Non-text portions of this message have been removed]

Reply via email to