Hi guys. I'm new in this community. I have to do a project which should do this:

Input an integer containing only 0s and 1s (i.e., a “binary integer) and print its decimal equivalent. [Use at most 5-digit integers.]

I checked forum but couldn't find something similar to or something works!

I did something like this, but teacher didn't accept it, because i have to do it with arrays, loops(for, do while or while).

Here is my job:

#include <stdio.h>
#include <stdlib.h>

/*BN means BinaryNumber
DN means DecimalNumber*/
main () {

int BN, DN ;
int a = 0 ,b, c, d, e ;

printf ("Please enter a number consists of 0s and 1s at most 5 digits:");
scanf ("%d", &BN);
if (BN / 10000 != 0) {
a = 1;
b = (BN - 10000) / 1000 ;
c = (BN - 10000 - 1000*b) /100 ;
d = (BN - 10000 -1000*b -100*c) / 10 ;
}
else {
a = 0;
b = BN / 1000;
c = (BN - 1000 * b) / 100;
d = (BN - 1000*b - 100*c) / 10;
}

e = BN %10;
DN = 16 * a + 8*b + 4*c + 2*d + e;

printf ("The Decimal Type of Number is : %d \n", DN);

system ("PAUSE");

return 0;

}

Please help, it is urgent.

Thanks.

S.

Member Avatar for iamthwee

If you don't know what a loop is you better look it up. Same goes for arrays and moving through them.

Once you've done that then you can approach the problem more clearly.

ThanQ

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

I know what loop, array mean. Teacher didn't tell us to do it with loops first, after i showed my job he told me to do it with loops. Anyway, i've done it.

Thanks for non-came helps. :)

S.

i can helpu with ur program

commented: Yeah, just so long as it doesn't involve reading the date, of say posts, say from THREE YEARS AGO! -7

Thanks buddy. This was urgent when I was at 1st grade at university. Now I am graduated and I don't need any help about this project. :) Thanks for your interest.

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.