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.



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. :)


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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.