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

Note: This method will NOT work for numbers larger than 1023

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.

26 thoughts on “C++ program to convert decimal number into binary number

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

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

  3. 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
    using namespace std;
    void main()
    int dec,rem,i=1,sum=0;
    cout< <"Enter the decimal to be converted:"<>dec;
    sum=sum + (i*rem);
    cout< <"The binary of the given number is:"<<sum<<endl;
    plz help me by giving my required code

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

  4. There you go Rupam.

    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.

  5. 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.

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

  7. 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?

  8. 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

  9. 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….

  10. 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.

  11. 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.

  12. 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.

  13. #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+3]= ‘+’;


    return binary;

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

    cout<< toBinary(value);

    return 0 ;

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

  15. Given code is working for converting a postive decimal number, can you provide me the code to convert negative decimal number also to binary number

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">