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.