Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Armstrong number in c programming

Tags:

c

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 even though 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 can I solve this?

like image 491
NAVANEETH J NAIR Avatar asked Sep 02 '25 03:09

NAVANEETH J NAIR


2 Answers

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.

like image 126
Eric Postpischil Avatar answered Sep 04 '25 16:09

Eric Postpischil


The problem is probably linked to the implementation of the pow() function on your system. Unless the implementors made a special case of integral arguments, pow(x, n) can be computed as exp(log(x) * n) which may produce a result that is just slightly less than the expected integral result because log(5) cannot be represented exactly as a floating point number (ie: 124,999999999999 instead of 125). Conversion to int is performed as truncation of the fractional part, hence 124 instead of 125.

Different implementations may produce slightly different results depending on how they handle integral arguments. This explains why you observe a problem and other people on different systems do not.

There are 2 ways to avoid this pitfall:

  • use round(pow(r, 3)) to ensure the result is the closest integer in case it is not already the expected integer.

  • use r * r * r which is performed with integer arithmetics.

Here is a modified version with the latter approach:

#include <stdio.h>

int main(void) {

    int n = 0;

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

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

    while (n != 0) {
        // storing the reminders of n divided by 10 in a variable
        int r = b % 10;
    
        // finding the power 
        sum = r * r * r + sum;
        b = b / 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 == n) {
        printf("The number is an armstrong number\n");
    } else {
        // if not then do this
        printf("It is not an armstrong number\n");
    }
    return 0;
}
like image 37
chqrlie Avatar answered Sep 04 '25 16:09

chqrlie