2

I am writing a program in which I have to check whether the number entered by the user is armstrong or not to do the calculation I am using the power function but there seems to be an error. I tried to check for the erorr in my code so i printed out rem for each iteration of the while loop, it turns out that when i give 153 as input (which is an armstrong number), the value of rem for the second iteration is coming out to be 124 but it should be 125(pow(5,3)).

This is my code :

// program to check if the number entered by the user is armstrong or not

#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
    int num, original_number, remainder, total = 0, num1, number_of_digits = 0, rem;
    printf("Enter the number you want to check :\n");
    scanf("%d", &num);
    original_number = num;
    num1 = num;
    while (num > 0)
    {
        num = num / 10;
        number_of_digits++;
    }
    while (num1 > 0)
    {
        remainder = num1 % 10;
        rem = pow(remainder, number_of_digits);
        printf("the rem is %d\n", rem);
        total = total + rem;
        printf("the total is %d\n", total);
        num1 = num1 / 10;
    }
    if (total == original_number)
    {
        printf("The number entered is an armstrong number");
    }
    else
    {
        printf("The number entered is not an armstrong number");
    }

    return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    `pow` is a floating point function, it may do [surprising things](https://stackoverflow.com/questions/588004/why-does-floating-point-arithmetic-not-give-exact-results-when-adding-decimal-fr), better make your own integer-based one – teapot418 Mar 01 '23 at 17:29
  • 2
    Use ordinary multiplication, not, `pow()`, for small integer exponents. And especially when the base is also an integer. – John Bollinger Mar 01 '23 at 17:29
  • https://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int – pm100 Mar 01 '23 at 17:34
  • 1
    Please confirm that your actual question is: _Why do does `pow(5,3)` result in 124 instead of 125?_. – Jabberwocky Mar 01 '23 at 17:44
  • 2
    Depending on your platform `pow(5,3)` might not be 125.0000000 but something like 124.99999999, which is truncated to 124 when converted to `int`. Do what the previous comments suggest. – Jabberwocky Mar 01 '23 at 17:49
  • If you want to use `pow`, then you need to round the result, e.g. `rem = round(pow(remainder, number_of_digits));` But as already mentioned, it's better just to write an integer pow function. – user3386109 Mar 01 '23 at 18:15

2 Answers2

3

In reviewing your code and the comments above, often times it comes down to the context of the problem being solved. In this case, since the program is working with integer values, and the "pow" function is meant for floating point functionality as noted in the good comments above, it would be wise to devise an alternative to using that function which only involves integers.

In this case with the context of what the program is trying to accomplish, one could write a small integer type power function, or even simpler, just utilize a simple loop to derive the power of an integer digit. Following is a quick refactored bit of code that replaces the "pow" function with a "for" loop.

    while (num1 > 0)
    {
        remainder = num1 % 10;
        /*  Replacement of the function with a loop */
        //rem = pow(remainder, number_of_digits);
        rem = 1;
        for (int i = 0; i < number_of_digits; i++)
        {
            rem = rem * remainder;
        }
        /* End of replacement */
        printf("the rem is %d\n", rem);
        total = total + rem;
        printf("the total is %d\n", total);
        num1 = num1 / 10;
    }

Testing this out with your value of "153" and with a four-digit test number results in the following terminal output.

@Vera:~/C_Programs/Console/Armstrong/bin/Release$ ./Armstrong 
Enter the number you want to check : 153
the rem is 27
the total is 27
the rem is 125
the total is 152
the rem is 1
the total is 153
The number entered is an armstrong number
@Vera:~/C_Programs/Console/Armstrong/bin/Release$ ./Armstrong 
Enter the number you want to check : 1634
the rem is 256
the total is 256
the rem is 81
the total is 337
the rem is 1296
the total is 1633
the rem is 1
the total is 1634
The number entered is an armstrong number

Keep in mind, this is just one of an assortment of methods that can be used to provide the desired solution. But give that a try and see if it meets the spirit of your project.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
0

By reviewing the comments above, I have written the program: Hope this helps

#include<stdio.h>
#include<math.h>
int main(){
    int n,i,cnt=0,sum = 0;
    printf("Enter number: ");
    scanf("%d",&n);
    for(i=n;i!=0;cnt++,i/=10);
    for(i=n;i!=0;sum += (int)round(pow(i%10,cnt)),i/=10);
    printf(sum==n?"Armstrong":"Not Armstrong");
    return 0;
}