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


    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.

  • doak

    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.

  • 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


    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 ;

  • maddy

    the logic needs to be changed to:

  • Dhruv

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

  • Sami Ahmed

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

  • kiran kumar

    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


    the answers comes wrong for the number 4567