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**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include using namespace std; int main() { long dec,rem,i=1,sum=0; cout<<"Enter the decimal to be converted:"; cin>>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; cin.get();="" return="" 0;="" }="" <="" pre=""> |

#### 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

#### Vlad

#### Latest posts by Vlad (see all)

- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013

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

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

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

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

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.

can anybody explain the logic used

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.

want a prog that do

1-hexadecimal to binary conversion

2-decimal to binary conversion

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

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?

Thanks for :DECIMAL to BINARY.

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

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

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.

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

nice logic

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.

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.

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

#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 ;

}

the logic needs to be changed to:

$rem=$a%10;

$sum=$sum+($i*$rem);

$a=$a/10;

$i=$i*2;

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

yeah!!!!!!!!

you rock man!!!!!

Keep it up.

thanks i was trying to do this but couldnt succeed, finally got it. thanksssss again

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

1023 and smaller numbers only