# Converting Decimal to binary

Page 1 of 1

## 5 Replies - 560 Views - Last Post: 03 October 2012 - 05:29 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=294021&amp;s=35a4733df6ae9e00a02c3bff8a66b802&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 exfoxs28

Reputation: 0
• Posts: 15
• Joined: 25-August 12

# Converting Decimal to binary

Posted 02 October 2012 - 06:19 PM

i've been trying to convert decimal to binary
im having a problem w/ the convertion the output is in reverse position..
when i input 10(decimal) the output must be 1010 but in my program the output is 0101

heres my code
```#include <iostream>
using namespace std;

int main()
{

long num, total=0,reverse[100],i;
cout << "enter a number: ";
cin >> num;
while(num>0)
{
total = num%2;
num /= 2;
cout << total;
}

system("pause>0");
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Converting Decimal to binary

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Converting Decimal to binary

Posted 02 October 2012 - 06:54 PM

That's because you're outputting the least significant digit first and the most significant digit last.

### #3 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

## Re: Converting Decimal to binary

Posted 02 October 2012 - 07:22 PM

Hi, you need to know the place.

If you are using the array after entering the number you can reverse it. Or you would need to know the size first, which can be found by repeatedly dividing the number by two until the number is zero and counting the size.

If you are using a number to store a binary representation, the place is the old place times 10.

0 -> 1
1 -> 10
2 -> 100
etc.

### #4 exfoxs28

Reputation: 0
• Posts: 15
• Joined: 25-August 12

## Re: Converting Decimal to binary

Posted 03 October 2012 - 04:55 PM

@Ctphpnwb thats my problem i dont know why the last digit came first...

@define i want to use array for this but i dont know how.. i already search in the net but theres no tutorial or topic about that..

### #5 jjl

• Engineer

Reputation: 1118
• Posts: 4,641
• Joined: 09-June 09

## Re: Converting Decimal to binary

Posted 03 October 2012 - 05:21 PM

The easiest "fix" would be to push all the values onto a stack, and then to print the binary, just loop through and pop the values off

Alternatively, whenever you find yourself "thinking backwards" that indicates you should maybe use recursion.

example
```void printBin(int num) {
int n = num % 2;
num /= 2;
if(num)
printBin(num); //repeat this funciton while num > 0
std::cout<<n;
}

```

This post has been edited by jjl: 03 October 2012 - 05:27 PM

### #6 #define

• Duke of Err

Reputation: 1520
• Posts: 5,315
• Joined: 19-February 09

## Re: Converting Decimal to binary

Posted 03 October 2012 - 05:29 PM