1

I was tasked with writing some code that will take a user input and convert the number to its binary number. I have written some code so far, but am having one issue. I have to use a for loop and the quotient-remainder method. When I output the remainder(binary), it is not printing the last digit.

The question I'm asking is: What would I have to change in my for loop to make it print out the last digit of the binary number?

int main()
{
    int num;
    int rem;

    cout << "Please enter a number: ";
    cin >> num;

    for (int i = 0; i <= (num + 1); i++)
    {
        num /= 2;
        rem = num % 2;
        cout << rem;
    }

    _getch();
    return 0;
} 

Any help is appreciated, Thank you!

Justin Farr
  • 183
  • 3
  • 5
  • 16
  • 4
    Possible duplicate of [C++ - Decimal to binary converting](http://stackoverflow.com/questions/22746429/c-decimal-to-binary-converting) – Stack Danny Oct 09 '16 at 18:55
  • just exchange both instructions `num /= 2;` and `rem = num % 2;` You start by dividing your number by 2 and so you lose the last binary number. – Franck Oct 09 '16 at 18:57

3 Answers3

5

You lose the last binary number when you start your algorithm by dividing num by 2. To avoid this issue, you should exchange both instructions num /= 2; and rem = num % 2;

Your loop also iterates too many times: in fact you can stop when num == 0. The following code is not valid for inputs that are <= 0.

int main()
{
    int num;
    int rem;

    cout << "Please enter a number: ";
    cin >> num;

    while (num != 0)
    {
        rem = num % 2;
        num /= 2;
        cout << rem;
    }
    cout << std::endl;

    return 0;
} 

If you want to write it in the right order, you should first compute the log of your number in base 2. The following solution uses a number index that starts with '1' and that has '0' after:

int main()
{
    int num;
    int rem;

    cout << "Please enter a number: ";
    cin >> num;

    if (num > 0) {
      int index = 1;
      while (index <= num)
         index *= 2;
      index /= 2;

      do {
        if (num >= index) {
           cout << '1';
           num -= index;
        }
        else
           cout << '0';
        index /= 2;
      } while (index > 0);
      cout << std::endl;
    }
    else
      cout << '0';

    cout << endl;
    return 0;
} 
Franck
  • 1,635
  • 1
  • 8
  • 11
0

You need to change your lines from

 num /= 2;
 rem = num % 2;

to

rem = num % 2;
 num /= 2;

This would print the binary number in reverse order. I would recommend changing the for loop to while(num>0) and adding each digit to an array instead of cout. Print the array from left to right later on to get the correct binary order.

Heisenberg
  • 5,514
  • 2
  • 32
  • 43
-2

The idea is to use math. Convert the base 10 integer to base 2. The other way maybe is to transverse the bits by testing the integer value against powers of 2 up to maximum bit value for integer. I also assume that you are not going to use floating point numbers. Converting to floating point binary values are a headache.

AdmiralSmith
  • 105
  • 2