# Very simple C++ question.. (Binary > Decimal)

Page 1 of 1

## 2 Replies - 711 Views - Last Post: 01 February 2012 - 02:20 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=265063&amp;s=cd1e5e616359c102c4a3a2597620b158&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lej

Reputation: 0
• Posts: 25
• Joined: 01-February 12

# Very simple C++ question.. (Binary > Decimal)

Posted 01 February 2012 - 01:22 PM

So I need to write a simple program that takes the user input (int) (Which will be in binary) , and outputs the decimal equivalent.

All we have learned so far is printf, scanf_s, if, do, while, int, and all the math functions.

We're suppose to do this WITHOUT the use of arrays, and WITH a loop.

I have no clue how to do this without arrays.. I know how to convert from binary to decimal and back on paper, but I don't know how to make the computer do this..

Any help would be greatly appreciated!

This is the format the teacher is expecting:

```#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
int binary, number;
printf("Enter binary number: ");
scanf_s("%d", &binary);

while ()
{
????????????
}

printf("Decimal equivalent is: %d", decimal);

return(0);
}
```

Is This A Good Question/Topic? 0

## Replies To: Very simple C++ question.. (Binary > Decimal)

### #2 vividexstance

• D.I.C Lover

Reputation: 755
• Posts: 2,658
• Joined: 31-December 10

## Re: Very simple C++ question.. (Binary > Decimal)

Posted 01 February 2012 - 01:41 PM

Here's a hint, look into the modulus operator(%) which returns the remainder after integer division. If you divide a decimal number by 10(ten), the result will be the same number without the rightmost digit. So if you have the number 1234 in decimal, to examine each individual digit, you could do something like this:
```int num = 1234;
int rem;

rem = num % 10;    /* rem = 4 */
num /= 10;         /* num = 123 */
rem = num % 10;    /* rem = 3 */
num /= 10;         /* num = 12 */
rem = num % 10;    /* rem = 2 */
num /= 10;         /* num = 1 */
rem = num % 10;    /* rem = 1 */
num /= 10;         /* num = 0 */

```

### #3 Lej

Reputation: 0
• Posts: 25
• Joined: 01-February 12

## Re: Very simple C++ question.. (Binary > Decimal)

Posted 01 February 2012 - 02:20 PM

Got it working now. Thanks