# Decimal to Binary Converter

Page 1 of 1

## 14 Replies - 2395 Views - Last Post: 12 August 2009 - 07:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=119798&amp;s=5c4e2e7d20ac9d93ff7d0c667bba5989&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shafqatbaboo

Reputation: -1
• Posts: 67
• Joined: 04-August 09

# Decimal to Binary Converter

Posted 12 August 2009 - 04:59 AM

I want to create a program that convert decimal number to binery
here is my code that not work:

```#include <iostream.h>
#include <iomanip.h>

using namespace std;

void Deci2Bin();

int main()
{
int number1;
int number2;

cout<<"======================================="<<endl;
cout<<"=	  Decimal to Binary Converter	="<<endl;
cout<<"=======================================\n"<<endl;

cout<<"Please enter first decimal number : ";
cin>>number1;
cout<<"Please enter second decimal number: ";
cin>>number2;

cout<<"======================================="<<endl;
cout<<"First Integer : "<<number1<<"="<<Deci2Bin<<endl;
cout<<"Second Integer: "<<number2<<"="<<endl;
return 0;
}
void Deci2Bin()

int num;
int bin[25];
int i;
for(i=0;num>0;i++)

{
bin[i]=num%2;
num/=2;
}
for(--i;i>=0;i--)
cout<<bin[i];

```

Is This A Good Question/Topic? 0

## Replies To: Decimal to Binary Converter

### #2 bimbolena

Reputation: 5
• Posts: 50
• Joined: 09-August 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:08 AM

Hi!

Deci2Bin must have a parameter for the number and be called as a function. I've changed that part below, hope it helps.

/Lena

```#include <iostream.h>
#include <iomanip.h>

using namespace std;

void Deci2Bin(int num);

int main()
{
int number1;
int number2;

cout<<"======================================="<<endl;
cout<<"=	  Decimal to Binary Converter	="<<endl;
cout<<"=======================================\n"<<endl;

cout<<"Please enter first decimal number : ";
cin>>number1;
cout<<"Please enter second decimal number: ";
cin>>number2;

cout<<"======================================="<<endl;
cout<<"First Integer : "<<number1<<"=";
Deci2Bin(number2);
cout<<endl;
cout<<"Second Integer: "<<number2<<"=";
Deci2Bin(number2);
cout<<endl;
return 0;
}
void Deci2Bin(int num)
{
int bin[25];
int i;
for(i=0;num>0;i++)
{
bin[i]=num%2;
num/=2;
}
for(--i;i>=0;i--)
cout<<bin[i];
}

```

### #3 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:34 AM

this should shed some light...

hope that helps

### #4 shafqatbaboo

Reputation: -1
• Posts: 67
• Joined: 04-August 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:40 AM

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

### #5 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:42 AM

which code?

### #6 kewlkreator

• D.I.C Lover

Reputation: 39
• Posts: 1,068
• Joined: 25-March 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 05:45 AM

DreamInCode has a request that you post your code like . This will keep your post organized and easy to tell apart. Please edit this.

### #7 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 06:05 AM

shafqatbaboo, on 12 Aug, 2009 - 11:40 AM, said:

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

in main()
```	cout<<"======================================="<<endl;
cout<<"First Integer : "<<number1<<"=";
Deci2Bin(number2);
cout<<endl;
cout<<"Second Integer: "<<number2<<"=";
Deci2Bin(number2);
cout<<endl;
return 0;

```

both times you call Deci2Bin() with number2 (hence you get 1000 printed both times) - the first time should be with number1, i.e.
```	cout<<"======================================="<<endl;
cout<<"First Integer : "<<number1<<"=";
Deci2Bin(number1);
cout<<endl;
cout<<"Second Integer: "<<number2<<"=";
Deci2Bin(number2);
cout<<endl;
return 0;
}

```

in Deci2Bin() a faster alternative to using the % operator
```			bin[i]=num%2;

```

is &, i.e.
```			bin[i]=num&1;

```

This post has been edited by horace: 12 August 2009 - 06:07 AM

### #8 tobix10

Reputation: 2
• Posts: 17
• Joined: 06-April 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:22 AM

``` for(--i;i>=0;i--)
cout<<bin[i];
```

look at this for statement

### #9 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:39 AM

edited

This post has been edited by computerfox: 12 August 2009 - 08:59 AM

### #10 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 08:58 AM

tobix10, on 12 Aug, 2009 - 02:22 PM, said:

``` for(--i;i>=0;i--)
cout<<bin[i];
```

look at this for statement

what is wrong with it? it prints out the binary value most significant bit first

### #11 Plus

• D.I.C Regular

Reputation: 41
• Posts: 414
• Joined: 24-November 08

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 09:42 AM

you know if you're writing a code for others to read, your code should make sense .. unless if you're making it complex for others to reas,

### #12 baavgai

• Dreaming Coder

Reputation: 7160
• Posts: 14,924
• Joined: 16-October 07

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:16 AM

Code runs fine. You can test it with a loop, if you like.

```#include <iostream>

using namespace std;

// pass a number
void Deci2Bin(int num) {
int bin[25];
int i;

for(i=0;num>0;i++) {
bin[i]=num%2;
num/=2;
}

// read it back out backwards
// note, need to decrement i first
for(--i;i>=0;i--) {
cout << bin[i];
}
}

int main() {
for(int i=0; i<20; i++) {
cout << i << "\t";
Deci2Bin(i);
cout << endl;
}
return 0;
}

```

A slightly cleaner way would be to use a string. That way you could also return it.

Which got me to thinking, if I really wanted to use an array for this, I'd go with a char array.

```void Deci2Bin(int num) {
const int MaxSize = 65; // you know, an int could be that big + 0
char s[MaxSize];
int pos = MaxSize; // init pos

// the trick with pos, it must always point to current
s[--pos] = 0; // terminate our string

while(num>0) {
s[--pos] = (num%2) ? '1':'0';
num/=2;
}

char *p = &s[pos]; // fake out cout
cout << p;
}

```

You can actually do a recursive solution to this in three lines. I'll leave that as an exercise for the reader.

This post has been edited by baavgai: 12 August 2009 - 11:16 AM

### #13 bimbolena

Reputation: 5
• Posts: 50
• Joined: 09-August 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:42 AM

Ooops my fault. Typed number2 twice when I made the answer for him. Or can I claim it wa intentional to check and see if he read and understand the code himself?

Lena

horace, on 12 Aug, 2009 - 05:05 AM, said:

shafqatbaboo, on 12 Aug, 2009 - 11:40 AM, said:

Thanx for helping but it gives me result show below:
when i enter 12 and 8 it give me same result that is 1000

in main()
```	cout<<"======================================="<<endl;
cout<<"First Integer : "<<number1<<"=";
Deci2Bin(number2);
cout<<endl;
cout<<"Second Integer: "<<number2<<"=";
Deci2Bin(number2);
cout<<endl;
return 0;

```

both times you call Deci2Bin() with number2 (hence you get 1000 printed both times) - the first time should be with number1, i.e.

### #14 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 11:52 AM

that's so mean lena lol

any further questions?

### #15 shafqatbaboo

Reputation: -1
• Posts: 67
• Joined: 04-August 09

## Re: Decimal to Binary Converter

Posted 12 August 2009 - 07:21 PM

Thanx very much.
Sir I also want to test AND,OR and Exclusive OR with these two number.
Help me for testing the result. Thankfull to all of you.