Convert Decimal Number Array to Binary using C++

This program converts all the decimal numbers in an array to binary.

To convert a decimal number into binary, we follow the following steps:
Divide the decimal number by 2 and note the remainder
Divide the Quotient repeatedly by 2 and note the remainders till quotient is 0
Write remainder side by side in reverse order to know the binary number
For example 18
18 divide by 2 leaves quotient 9 remainder 0
9 divide by 2 leaves quotient 4 remainder 1
4 divide by 2 leaves quotient 2 remainder 0
2 divide by 2 leaves quotient 1 remainder 0
1 divide by 2 leaves quotient 0 remainder 1

So binary of 18 is 10010

The Program:

Output:

The following two tabs change content below.

Lalit Mali

Lalit is a technology enthusiast, a programming lover and currently an Android fan.
  • HelloC++

    I have a question:
    On line 25, the condition for the ‘while’ statement says that CurrentDec has to be greater than 0. The first number of the DecimalArray is 1(which is also stored in CurrentDec variable) and when it’s divided by 2, it becomes 0 and doesn’t ever enter the while loop. Am I missing something?