Hello experts.
I have an issue.
The exercice: define, if a number that user enter (positive, even and non zero) is an Armstrong Number.
* Armstrong number - number, which equal sum of digits, that compose this number
to the power equal to the number of digits.
For example: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27.
Sequence of Armstrong Numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371,
407, 1634, 8208...
I Use Code Blocks and Qt Creator.
The problem is, that in Code Blocks for only 153 i have an Armstrong sum = 152
(27 + 125 = 151 from debug) (and if operator return false, but 153 - Armstrong Number), the same code in Qt Creator return correct result - 153.
What could be the reason?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <iostream>
#include <math.h>
using namespace std;
int main()
{ int N = 0, n = 0, Armstrong_sum = 0,i = 1;//variables definition
cout << "Please input number:" << endl;//hello message
cin>>N;//number input
n = N; //number buffer
while(n>10)//loop for checking how many digits in number
{
i++;
n = n/10;
};
n = N;//again number buffer
while(n>10)//Armstrong number sum loop
{
Armstrong_sum = Armstrong_sum + pow(n%10,i);//power in a i
//checking the sume
cout<<"Pow of "<<n%10<<" to "<<i<<" = "<<pow(n%10,i)<<endl;
cout<<"Sum = "<<Armstrong_sum<<endl;
n = n/10;
};
//powering the last from end number to i, or only number
cout<<pow(n%10,i)<<endl;
Armstrong_sum = Armstrong_sum + pow(n,i);
//output the Armstrong Sum
cout<<"Armstrong`s Sum for power "<<i<<" = "<<Armstrong_sum<<endl;
//number check
if(Armstrong_sum == N) cout<<"Number is Armstrong`s number";
else cout<<"Number is not Armstrong`s number";
return 0;
}
|