2 Replies - 625 Views - Last Post: 01 February 2012 - 02:20 PM Rate Topic: -----

#1 Lej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,357
  • 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 */


Was This Post Helpful? 2
  • +
  • -

#3 Lej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1