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
}

Recommended Answers

All 9 Replies

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.}

#include <stdio.h>


{

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, etc)
r += digit * mult; // multiply the previous calc with digit
number /= 10; // remove the last digit
}
return r;
}
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 base)
		mul = (int)pow((double)base,(double) i++); //calculate base ^ i (0, 1, 2,
		r += digit * mul;
		number/=10;
	}
	printf("%d hi-",r);
return r;
}

i think thats what u were asking...

commented: Giving away the homework answer, and with overly complicated code -2
commented: Incorrect Mr WaltP. Same code OP posted. +6

posting same question twice does not solve u'r problem.

hey thanks for the help but i keep getting this error

new.c: In function 'base_numeric':
new.c:12: warning: incompatible implicit declaration of built-in function 'pow'
new.c:16: warning: incompatible implicit declaration of built-in function 'printf'
/usr/bin/ld: Undefined symbols:
_main
collect2: ld returned 1 exit status

DangerDev
Not only do we not do other people's homework for them, we try to
1) help them come up with their own answers
2) use good code. What the h*** is mul = (int)pow((double)base,(double) i++); :icon_question:
What's wrong with a simple r = (r * base) + digit; :icon_question:
What do we need doubles for?

wollacott:
#1) Format your code.
#2) Where is main() defined?
#3) Don't you need some kind of header for pow() .
#4) Why are you using pow() anyway? You don't need doubles for this program.
#5) Don't make a new thread for the same program -- reply to the original thread.
#6) You can't possibly get an error about a function you don't use ( printf() )

>2) use good code. What the h*** is
mul = (int)pow((double)base,(double) i++);

i m using vc++ there pow function takes double as argument so its called type casting.


>#2) Where is main() defined?
>#3) Don't you need some kind of header for pow().

i thought its common sense.

>2) use good code. What the h*** is


this is not my code, i have just modified it.

>2) use good code. What the h*** is
mul = (int)pow((double)base,(double) i++);

i m using vc++ there pow function takes double as argument so its called type casting.

So? Turbo C has pow() too, but you don't need it for this program. See the rest of my post you quoted.

>#2) Where is main() defined?
>#3) Don't you need some kind of header for pow().

i thought its common sense.

So why didn't he use them? Didn't you notice the post was not directed to you.

>2) use good code. What the h*** is

this is not my code, i have just modified it.

That'll teach you to use someone else's code you don't understand to try to help someone... :icon_wink:

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.