You are using a version without Ads of this website. Please, consider donating:

### Armstrong number

Hi I am very new to c++ programming.I am just trying to check whether the number is Armstrong number. Please see my code below.

This program works fine for all the numbers but when I give 153, which is Armstrong number is getting printed as not an Armstrong number.
Also armstrong=armstrong+pow((temp%10),counter); gets evaluated as 124 instead of 125 for 5^3.

Eventhough I have found better methods now. I still want to know why this code isn't working.

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

using namespace std;

int armstrong(int n)
{
int counter=1;
int temp =n;
int armstrong=0;
while(n>10)
{
n=n/10;
counter ++;
}
while(temp>=1)
{
armstrong=armstrong+pow((temp%10),counter);
temp=temp/10;
}

return armstrong;
}

int main()
{
int n=0;
cout << "Enter the number to check!" << endl;
cin >> n;
armstrong(n);
if(n==armstrong(n))
{
cout << "The number is armstrong"<< endl;
}
else
{
cout<< "The number is not armstrong"<<endl;
}
return 0;
}

> but when I give 153, which is Armstrong number is getting printed as not an Armstrong number.
> Also ... gets evaluated as 124 instead of 125 for 5^3.

The floating point representation is an inexact representation of a real number; because of its inexactness, computations involving floating point values are subject to rounding errors.

See: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
http://www.petebecker.com/js/js200007.html

Use integer arithmetic instead, and the program should be fine.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include // #include int pow( int base, int exponent ) { if( exponent < 0 ) return 0 ; int result = 1 ; while( exponent > 0 ) { result *= base ; --exponent ; } return result ; } //using namespace std; int armstrong(int n) { int counter=1; int temp =n; int armstrong=0; while(n>10) { n=n/10; counter ++; } while(temp>=1) { armstrong = armstrong+pow((temp%10),counter); temp=temp/10; } return armstrong; } int main() { int n=0; std::cout << "Enter the number to check!" << '\n' ; // endl; std::cin >> n; //armstrong(n); if( n == armstrong(n) ) { std::cout << "The number is armstrong" << '\n' ; // endl; } else { std::cout<< "The number is not armstrong" << '\n' ; // endl; } //return 0; }``````
Thanks a lot! It works now. since the Pow function inside the math.h converts the int to double value. The conversion and rounding led to surprising results.

Correct?

Also is there any best practice to avoid this kind of errors in future
Why I am so noob and I cannot help anyone?
Anyway, I am beginer.
> since the Pow function inside the math.h converts the int to double value.
> The conversion and rounding led to surprising results. Correct?

Yes.

> is there any best practice to avoid this kind of errors in future

There are several specialized techniques to minimise floating point errors.

Some are quite simple; for instance, compensated summation: http://en.wikipedia.org/wiki/Kahan_summation_algorithm

Others are more involved; for instance those described in this paper: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

For now, just keep in mind:

1. Use integer arithmetic where possible.

2. Avoid simple equality comparisons - `if( a == b )` - on floating point values.
See: http://floating-point-gui.de/errors/comparison/
Topic archived. No new replies allowed.