1

I am not able to convert from decimal to binary in C.Everytime I get a output which is one less than the desired output.For ex.:5 should be 101 but shows up as 100 or 4 should be 100 but shows up as 99.

#include<stdio.h>
#include<math.h>
void main() {
    int a,b=0;
    int n;
    printf("Enter a Decimal Number\n");
    scanf("%d",&n);
    for(int i=0;n>0;i++) {
        a=n%2;
        n=n/2;
        b=b+(pow(10,i)*a);
    }
    printf("%d",b);
}

My output is always one less than the correct answer and I dont know why.It fixes the problem if take b as 1 instead of 0 in the beginning but i dont know why.Please Help.I have just started C a few days ago.

11
  • 2
    I recommend you use bitwise operations instead. Get the top bit using bitwise AN, and print it. Then shift the value up one bit and so on. Commented Nov 25, 2022 at 16:52
  • 2
    Using pow for integer math is usually a bad sign. Commented Nov 25, 2022 at 16:52
  • 1
    Also, when dealing with bits I really recommend you use unsigned integers. Commented Nov 25, 2022 at 16:52
  • 1
    To convert a decimal number to binary, a simple b = d; suffices. (More to the point, d was binary already, if it has a meaningful base at all.) If you want to construct the binary representation of an integer, don't construct it in an integer. You want it as a string. The first decimal number I tried your code on was 1234, and it converted it to -2147483648, which is not the correct binary representation. Commented Nov 25, 2022 at 17:01
  • 1
    cannot reproduce Commented Nov 25, 2022 at 17:04

2 Answers 2

1

pow is a floating-point function; it takes a double argument and returns a double value. In the C implementation you are using, pow is badly implemented. It does not always produce a correct result even when the correct result is exactly representable. Stop using it for integer arithmetic.

Rewrite the code to compute the desired power of ten using integer arithmetic.

Also, do not compute binary numerals by encoding them a decimal within a int type. It is wasteful and quickly runs into bounds of the type. Use either bits within an unsigned type or an array of char. When scanf("%d",&n); executes, it converts the input string into binary and stores that in n. So n is already binary; you do not need to decode it. Use a loop to find its highest set bit. Then use another loop to print each bit from that position down to the least significant bit.

Sign up to request clarification or add additional context in comments.

Comments

0

This code seems fine. I quickly tested it on an online compiler and it seems to be working okay.

I am very sure it has to do with different versions of compilers. compiler which I tested your code in: https://www.onlinegdb.com/online_c_compiler

Edit:

pow() function is not reliable when used with integers since the integer you pass into it as parameter is implicitly converted into data type of double and returns double as output. When you stuff this value into the integer again, it drops the decimal values. Some compilers seem to produce "correct" result with their version of pow() while some don't.

Instead, you can use a different approach to solve your decimal to binary conversion without errors in general use:

#include<stdio.h>
void main() {
    int remainder,result = 0,multiplier = 1;
    int input;
    printf("Enter a Decimal Number\n");
    scanf("%d",&input);
    while(input){
        remainder = input%2;
        result = remainder*multiplier + result;
        multiplier*=10;
        input/=2;
    }
    printf("The binary version of the decimal value is: %d",result);
}

5 Comments

Although the code worked for you, it is poor code. It might seem to work, but not for the right reasons. It is not guaranteed to work. Its usage of the problematic pow() function, though superficially attractive, is a potentially-fatal limitation.
And to illustrate another of this code's problems, try using it to convert the number 12345, or even just 1234. You probably won't get 11000000111001 and 10011010010.
@SteveSummit I understand where you are coming from. But keeping in mind that the user is trying to figure out why their code is not working with numbers like 4 and 5, you cannot just blame the pow() function here. It is completely irrelevant.
On the contrary. The OP's problems with numbers like 4 and 5 are precisely because of his system's version of pow(). It's doing things like computing pow(10, 2) as 99.999999999999985.
@SteveSummit Seems like we can answer this in both ways. We can either blame the function or certain compilers. I admit the unusual behavior of pow() function, hence I have updated my answer. Thank you for your input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.