1

I made a program in c to find whether an integer is an armstrong number or not for three digits numbers. If the number that has been given by the user is an armstrong number it will print it is an armstrong number and if it is not an armstrong number it will print its not an armstrong number. It is not showing any errors. Also it works with all three digit numbers except for a number. It is 153. It is printing that it is not an armstrong number eventhough it is an armstrong number. Could anyone tell me whats wrong here. here is the code and output:

#include <math.h>
#include <stdio.h>
int main(){

    int sum = 0;
    int n;

    //asking the user to enter a number
    printf("Enter a number : ");
    scanf("%d",&n);

    // copying the value of n in a variable b
    int b = n;


    while(n!=0){
        // storing the reminders of n divided by 10 in a variable
        int r = n%10;
    
        // finding the power 
        sum = pow(r,3)+sum;
        n = n/10;
    }
    // here i am printing the sum just to show you guys what is the answer i am getting
       printf("%d\n",sum);

       // if sum equals to b, then do this
       if (sum==b){
           printf("The number is an armstrong number\n");
    }
        // if not then do this
       else{
           printf("It is not an armstrong number\n");
    }
}

Here is the output i am getting if i enter a three digit armstrong number:

Enter a number : 370
370
The number is an armstrong number

Enter a number : 407
407
The number is an armstrong number

Here i am entering a number which is not an armstrong number:

Enter a number : 100
1
It is not an armstrong number

153 is an armstrong number. Here is what happens when i enter 153:

Enter a number : 153
152
It is not an armstrong number

It is saying 153 is not a armstrong number. Why does this happen? How to solve this? please reply

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • I think your problem, regarding the inaccuracies of pow and floating point, is answered [here](https://stackoverflow.com/questions/18155883/strange-behaviour-of-the-pow-function) – mmixLinus Mar 18 '22 at 15:12

2 Answers2

3

The pow implementation in your standard C library is deficient and returns an approximation even when the mathematical result is exactly representable.

Stop using it for cubes. Replace pow(r,3) with r*r*r.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • The problem is that `pow` is a floating-point function which by its very nature is imprecise. Sometimes rounding error might, for example, make `pow(5,3(` come out to 124.99999999999, which, when converted back to an integer, becomes 124 instead of 125. This solution is correct. – SGeorgiades Mar 18 '22 at 15:14
  • @SGeorgiades: No, that is not the problem. It is entirely possible to implement `pow` so that it returns exactly correct results for representable values. For example, the macOS `pow` implementation does that. Saying this is the problem is like saying you should expect the integer `/` operator to be wrong because it is by its very nature imprecise: It must truncate `7/3` to 2, therefore you should expect `9/3` to be wrong. That is an incorrect argument; the fact that some mathematical results cannot be represented does not mean others must be calculated incorrectly. – Eric Postpischil Mar 18 '22 at 15:18
  • @SGeorgiades: The explanation is as I stated in the answer: The `pow` implementation in OP’s standard C library is deficient. Microsoft’s `pow` implementation was known to have this deficiency in previous versions (maybe in some current software; I do not know what they have deployed currently). There is no mathematical reason for this; it is an implementation choice about what results to deliver. It is **not** the “very nature” of the function. It is an implementation choice. – Eric Postpischil Mar 18 '22 at 15:44
  • Are you saying that it is the problem of operating system – NAVANEETH J NAIR Mar 19 '22 at 03:59
  • @NAVANEETHJNAIR Its not an issue with the operating system. its the compilers how they implement that. – balu Mar 19 '22 at 04:18
1

Your code is working on gcc compilers. check for yourself https://godbolt.org/z/Pvo19xrxx

as mentioned by others use r*r*r instead of pow.

balu
  • 1,023
  • 12
  • 18