C++ program to convert decimal number into binary number

This following program converts a decimal number to a binary number.
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

What happens in the Do While loop:

Step 1. Divide the number in decimal format by 2 and save the remainder in “rem”

Step 2. Place the remainder in the variable sum. We multiply our remainder by “i” so it falls in the right place. i.e, Ones place in 1st time, tens at 2nd and so on.

Step 3. Halve the number in decimal format and save it back to “dec”

Step 4. Multiply “i” by 10 so the next number is placed one place higher in the binary number (i.e, hundreds after tens and thousands after hundreds).

Step 5. Repeat the above steps till decimal number reaches 0

The following two tabs change content below.
  • 10 gallon fish tank

    Hello thank you for publishing this information, I was going to attempt something somewhat the same so this information is useful. -Bobby A.

  • Aiman

    how to convert decimal to binary using function char * convertDecimalToBinary(int value) and void convertDecimalTiBinary (int value,char * s)

  • hamdu

    10 q for giving me this information

  • RUPAM GANGULY

    I have a code for convertin a decimal number to its equivalent binary nuber.but I want to apply the same code for an array to get the binary conversion of all its elements.
    The #include
    #include
    using namespace std;
    void main()
    {
    int dec,rem,i=1,sum=0;
    cout< <"Enter the decimal to be converted:"<>dec;
    do
    {
    rem=dec%2;
    sum=sum + (i*rem);
    dec=dec/2;
    i=i*10;
    }while(dec>0);
    cout< <"The binary of the given number is:"<<sum<<endl;
    getch();
    }
    plz help me by giving my required code

    The code given here only gives the binary of a single element.

  • doak

    There you go Rupam.

    Convert Decimal Number Array to Binary using C++:
    http://www.mycoding.net/2012/02/convert-decimal-number-array-to-binary-using-c/

    This program uses 2 arrays for decimal and binary and then the same logic from above to convert each number.

  • Engr Immi

    can anybody explain the logic used

  • doak

    Hey Engr Immi

    We’ve now added an explanation for the logic in the post itself.
    Please let us know if its not clear enough.

  • henrich

    want a prog that do
    1-hexadecimal to binary conversion
    2-decimal to binary conversion

  • Kamwesigye isaac

    some body help me to create a program in c++ for converting binary numbers to decimal and decimal numbers to binary.

  • bakari kassim mzengaeka

    thanks alot! I Was tried in my level best but my program was converting only decimal numbers from 0 to 9. How can i write the same code using array?

  • Janko97

    Thanks for :DECIMAL to BINARY.

  • Thedarkknigth

    Your code does not function properly
    first wen i use 12 it worked but the second time wen i entered 79 it displayed 187091
    which is incorrect

  • Suraj

    Its so simple program for understanding by students…keep it up with maximum programs of c…
    its good to have such sites for students to refer for programs without any cost…thanks for sharing various programs in public….

  • doak

    Hey Thedarkknigth,
    I tested the program again and it works just fine. Even for the input 79.

    Plus, I’ve updated the program and removed conio.h header file and its functions. Also, the program is now int main() instead of void.

  • doak

    Thanks a lot Kassim, Janko, Suraj and everybody else. It’s always good to know it helped someone.

  • thanx

    nice logic

  • SomnathR

    Hey doak, Thedarkknigth’s right. The problem arises when we take any number more than 31. In C++, the maximum no. that int data type can store is 32767. But numbers more than 31 have six digits when they are converted to binary. So, change the int to long and the program works fine.

  • doak

    Thanks for pointing that out SomnathR
    Although numbers greater than 31 are working fine on my machine, it maybe causing that problems on other people’s machine or their compiler.
    I’ve modified the program to use long in place of int.

  • dan

    Also long long can be used for larger numbers such as 1234 ;)

  • Jacques

    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;

    char* toBinary(char* doubleDigit)
    {
    int digit = atoi(doubleDigit);
    char* binary = new char();

    int x = 0 ;
    for(int i = 9 ; digit != 0; i–)
    {
    //cout << "i"<< i<=0)
    {
    digit = digit- pow(2,i);
    binary[x]= ’2′;
    binary[x+1]=’^';
    binary[x+2]=i+’0′;
    binary[x+3]= ‘+’;
    x+=4;

    }

    }
    return binary;
    }

    int main()
    {
    char value[3]={’8′,’2′,’0′};

    cout<< toBinary(value);

    return 0 ;
    }

  • maddy

    the logic needs to be changed to:
    $rem=$a%10;
    $sum=$sum+($i*$rem);
    $a=$a/10;
    $i=$i*2;

  • Dhruv

    But this is not useful when converting 10.12 to binary.Please give me a code for that.

  • Sami Ahmed

    yeah!!!!!!!!
    you rock man!!!!!
    Keep it up.
    thanks i was trying to do this but couldnt succeed, finally got it. thanksssss again :)