0

For some values (like 9) it works perfectly but, for most (like 7, 19 or 6), it subtracts 1 from the return (binary) value.

#include<iostream>
#include<cmath>
using namespace std;
int decimaltobinary(int);
int main()
{
    int num;
    cout<<"Enter the number: ";
    cin>>num;
    cout<<num<<" in decimal = "<<decimaltobinary(num)<<" in binary.";

    return 0;
}
int decimaltobinary(int num)
{
    int remainder,i=0,binary=0;
    while(num!=0)
    {
        remainder=num%2;
        num=num/2;
        binary=binary+remainder*pow(10,i);
        i++;
    }
    return binary;
} 
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • The problem is that you are confused about which language you are programming in. The code is C++, but you have tagged the C language. FYI, the C language doesn't have `std::cout` nor `std::cin`. I recommend you updating your language tags. – Thomas Matthews Sep 12 '21 at 18:40
  • 1
    `pow` works with floating point numbers, which are not exact. It may be introducing an off-by-one error you are seeing. If you start `i` at `1` you can multiply it by `10` instead of incrementing. Then you can use `i` instead of `pow(10,i)` which would only use integers. – François Andrieux Sep 12 '21 at 18:41
  • 1
    Decimal and binary are **text** representations of values. Converting a value to its binary representation means converting it to text. – Pete Becker Sep 12 '21 at 18:41
  • Instead of `pow(10, i)`, just accumulate the multiplier: `int i = 1;` before the loop, and in the loop do `binary = binary + i * remainder; i *= 10;`. – Pete Becker Sep 12 '21 at 18:44
  • The program works fine for me (if num<1024 but that is because of the int overflows) Are you sure for 6 it outputed 101 for you? – Botond Horváth Sep 12 '21 at 18:50

2 Answers2

1

There are two main problems with the shown code:

  1. The shown code attempts to build a binary version of the input number in decimal producing, for example, the result of 111 for the number 7. That's an integer value of one hundred and eleven.

On a 32 bit platform, with a 32 bit integer means that the largest number that can be "converted" to decimal this way will be 2047. 2048 is 10000000000 in binary, which will exceed the capacity of a 32 bit integer. An unsigned 32 bit integer's maximum value is 4294967295 (and half of that for it's plain, signed, int value, but either signed or unsigned, you're out of gas at this point).

  1. Any use of pow() with two values that are integets is automatically broken by default, becase floating point math is broken. This is not what pow() really does. Here's what pow() does: a) it takes the natural logarithm of its first parameter, b) multiples the result from step a by its 2nd parameter, c) raises e to the power resulting from step b. Does this sound like something you expected to do here?

And since pow() takes floating point parameters, and the result is a floating point, the end result of the shown code is a bunch of needless conversions between floating point and integer values, and non-specific rounding errors as a result of imprecise floating point exponential math.

But the main flaw in the shown code is an attempt to use plain ints to assemble a decimal number represented of a binary value, which simply doesn't have enough digits for this. Switching to long long int won't be much of a help. Counting things off on my fingers, you'll be able to go up only to somewhere slightly north of a million, that way. A completely different approach must be taken for the described programming tasks.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

Your problem is that binary+remainder*pow(10,i); is all done in floating-point arithmetic and only converted to int at the assignment. Since pow is not exact, you may get the result slightly below the exact value, in which case the conversion truncates it and makes 1 less than the desired result.

While there are various better ways to achieve your goal, the immediate fix is to use std::round() and then cast the result to int:

binary=binary+remainder*int(round(pow(10,i)));
Eugene
  • 6,194
  • 1
  • 20
  • 31