Problem with Code Blocks

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;
}
I've not tested this, but possibly the implementation of the pow() function uses floating point values, and the returned result may be something like 2.99999 instead of 3.00000 (just an example) and hence there may be rounding or truncation errors. You might consider either a solution which does not use pow at all, or perhaps make your own version to handle integer values. Maybe.
Thanks, a lot, Chervil, it help.
I use case operator for powering.
So i can`t use pow() for int type of variables?

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
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

int main()
{   int N = 0, n = 0, Armstrong_sum = 0, Armstrong = 0;
    int i = 1;//variables definition
    cout << "Please input number:" << endl;//hello message
    cin>>N;//number input
    n = N; //number buffer

    while(n>10)//division loop at 10 and checking if number even or odd

      {
       i++;
       cout<<i<<"\n";
       n = n/10;
       };
     n = N;
     while(n>10)//Armstrong number sum loop

      {
      //powering
       switch(i)
        {
        case 1: Armstrong = n%10;break;
        case 2: Armstrong = (n%10)*(n%10);break;
        case 3: Armstrong = (n%10)*(n%10)*(n%10);break;
        case 4: Armstrong = (n%10)*(n%10)*(n%10)*(n%10);break;
        }

       Armstrong_sum = Armstrong_sum + Armstrong;
       cout<<"Pow of "<<n%10<<" to "<<i<<" = "<<Armstrong<<endl;
       cout<<"Sum = "<<Armstrong_sum<<endl;
       n = n/10;
       };
       //powering
       switch(i)
        {
        case 1: Armstrong = n%10;break;
        case 2: Armstrong = (n%10)*(n%10);break;
        case 3: Armstrong = (n%10)*(n%10)*(n%10);break;
        case 4: Armstrong = (n%10)*(n%10)*(n%10)*(n%10);break;
        }
       cout<<Armstrong<<endl;
       Armstrong_sum = Armstrong_sum + Armstrong;

       cout<<"Armstrong`s Sum for power "<<i<<" = "<<Armstrong_sum<<endl;
       if(Armstrong_sum == N) cout<<"Number is Armstrong`s number";
       else                   cout<<"Number is not Armstrong`s number";


    return 0;
}
So i can`t use pow() for int type of variables?

As you found, it may be implementation-dependent. If pow() is considered the best solution, then you might then round() the result to the nearest integer.

http://www.cplusplus.com/reference/cmath/round/

Thanks a lot. Final work code:
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
46
47

#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

      {
       //powering a digit to power i and sum with rounding float to int 
       Armstrong_sum = Armstrong_sum + round (pow(n%10,i));

       //checking the sum
       cout<<"Pow of "<<n%10<<" to "<<i<<" = "<<round (pow(n%10,i))<<endl;
       cout<<"Sum = "<<Armstrong_sum<<endl;
       n = n/10;
       };

       /*powering the last from end number to i, or only 1 digit number to i with rounding float   
          to int */
       cout<<round (pow(n%10,i))<<endl;
       Armstrong_sum = Armstrong_sum + round (pow(n%10,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;
}
Hi,

Did you know there are compound assignment operators?

1
2
n /= 10;  //n = n/10;
Armstrong_sum += round (pow(n%10,i));  // Armstrong_sum = Armstrong_sum + round (pow(n%10,i)); 


There are lots of them :

http://en.cppreference.com/w/cpp/language/expressions#Operatorsb

Hope this helps you out a bit :+)
I know about this but i clear see only increment and decrement, when code or see another code, another operations now i clear understand when use full operator and equation:

 
Armstrong_sum = Armstrong_sum + round (pow(n%10,i));


Maybe in a future, when i enjoy some better skills, i will begin use compound operators.

Thanks a lot for support!
Topic archived. No new replies allowed.