Input an integer containing only 0’s and 1’s( ie. “binary” integer) and print its decimal equivalent. {hint: use the remainder and division operators to pick off the binary numbers digits one at a time from right to left. Just as in the decimal number system, in which the right most digit has a positional value of 1, and the next digit has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 4*1+3*10+2*100. The decimal equivalent of binary 1101 is 1*1+0*2+1*4+1*8 or 1+0+4+8 or 13.}

int base_numeric() { works with any base 2-10

int number = 1101; binary number

int base = 2; from base 2

int i = 0;

int r = 0;

while (number > 0) {

int digit = number % 10; extract 1 digit (no matter base)

int mult = (int)pow(base, i++); calculate base ^ i (0, 1, 2,

r += digit * mul

number/=10;

}

return r

}

wollacott
0
Light Poster

## Recommended Answers

Jump to Post`int base_numeric() { //works with any base 2-10 int number = 1101; //binary number int base = 2; //from base 2 int i = 0; int r = 0; int mul; while (number > 0) { int digit = number % 10; //extract 1 digit (no matter …`

Jump to Postposting same question twice does not solve u'r problem.

Jump to PostThere is no pow (int, int) function. You have to write your own.

http://www.cplusplus.com/reference/clibrary/cmath/pow.html

As for the printf warning, I don't see that in your code, so I can't comment.

## All 9 Replies

wollacott
0
Light Poster

DangerDev
107
Posting Pro in Training

WaltP
commented:
Giving away the homework answer, and with overly complicated code
-2

Aia
commented:
Incorrect Mr WaltP. Same code OP posted.
+6

DangerDev
107
Posting Pro in Training

wollacott
0
Light Poster

VernonDozier
2,218
Posting Expert
Featured Poster

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

DangerDev
107
Posting Pro in Training

WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.