A binary number is a sequence of bits (binary digits – 0’s and 1’s) of the form

BnBn-1……B1 B0, where each Bi is a bit.

The decimal equivalent is calculated by Bn * 2n+ Bn-1 * 2n-1 + … + B1 * 2 + B0.

Write a program to input a binary number and output the decimal equivalent.

The sample input will not have more than 8 individual bits (i.e., the largest value to be entered is 11111111).

**Example 1**

Enter binary number: 101

In decimal: 5

**Example 2**

Enter binary number: 11111

In decimal: 31

Enter binary number: 11111111 In decimal: 255 Enter binary number: 10101010 In decimal: 170 Enter binary number: 0 In decimal: 0 Enter binary number: 1 In decimal: 1 Enter binary number: 10 In decimal: 2 Enter binary number: 11 In decimal: 3 Enter binary number: 100 In decimal: 4 Enter binary number: 10000000 In decimal: 128

// @BEGIN_OF_SOURCE_CODE #include <cstdio> #include <cstring> using namespace std; /** * we could use pow() function for calculating power * pow() function returns double * sometimes double can produce ambiguous result * for example, pow (2, 3) = 8.000000000001 * lets keep it simple and write our own power() function */ int power (int base, size_t exponent) { int ret = 1; for ( int i = 1; i <= exponent; i++ ) { ret *= base; } return ret; } int main (int argc, char *argv []) { char binary [9 + 3]; printf ("Enter binary number: "); scanf ("%s", binary); size_t length = strlen (binary); // "size_t" is also known as "unsigned long" int decimal = 0; for ( int i = 0; i < length; i++ ) { int binaryDigit = binary [i] - '0'; decimal += (binaryDigit * power(2, length - 1 - i)); } printf ("In decimal: %d\n", decimal); return 0; } // @END_OF_SOURCE_CODE

### Decimal to Binary conversion

// @BEGIN_OF_SOURCE_CODE #include <cstdio> #include <algorithm> using namespace std; int main(int argc, const char * argv[]) { int n; scanf ("%d", &n); int binary [10 + 5]; int index = 0; while (n) { binary [index] = n % 2; index++; n /= 2; } reverse(binary, binary + index); for ( int i = 0; i < index; i++ ) printf ("%d", binary [i]); if (index == 0) printf ("0"); printf ("\n"); return 0; } // @END_OF_SOURCE_CODE

Advertisements

I’m pretty new to programming, and here’s my contribution in Python. Sorry if my style is messy/confusing, as I’m not completely familiar with the conventions for commenting, spacing, etc. If you have any suggestions or simplifications, they’re definitely appreciated:

Sorry for the really bad formatting of the above post, as well. I’m not positive how to format it properly so that it actually looks like code.

@Colin,

hi. you program doesn’t produce correct results

for example, if input is 11 output should be 3

and if input is 101 output should be 5

try to debug your program .. or even better, write and justify your logic in a pen-paper

for your commenting concern, i can say that, try to write code where you need to comment at minimal.

what i mean is, your variable name should be more descriptive and your logic should be simple

variable names, ‘a’, ‘b’ or ‘n’ doesn’t actually carry any information than ‘average’ / ‘input’ or ‘length’

and one thing .. a famous jargon in coding world .. ‘your comment should ans the question ‘why’ .. not ‘what’

just for example, ‘binary = raw_input(‘Please enter a binary number: ‘) #asks user for binary input’

is an unnecessary comment .. we can all see you are taking input

i am not a good programmer either .. still i hope the reply may help .. Best of luck to you 🙂

Haha…oops. I just realized that I wrote a program to convert bin–>dec and not dec–>bin

Actually, I just re-read the problem that you posted, and it says that it is solely supposed to be a bin to dec converter, and not both a dec to bin converter, as well. So when you initially put in the input of 11, you’re not going to get an output of 3. However, it does convert 101 to 5 quite capably, as I just ran my program again.

@Colin

yes the problem is all about binary to decimal conversion. you do not need to convert decimal to binary

and well … binary (11) = decimal (3)

we can do this simply:

#include

#include

int main()

{

int bin,dec,i,j,sum=0;

printf(“Enter binary number:”);

scanf(“%d”,&bin);

i=0;

while(bin!=0)

{

j=bin%10;

sum=sum+j*pow(2,i);

bin=bin/10;

i++;

}

printf(“In decimal:%d”,sum);

return 0;

}

Am new to programming so can someone please explain what i did wrong

string binary = Console.ReadLine();

char[] character = binary.ToCharArray();

Array.Reverse(character);

string values = character.ToString();

List componenets = new List();

List results = new List();

double Denary = 0;

for (int i = 0; i < binary.Length; i++)

{

componenets.Add(values.Substring(i, 1));

}

foreach (string X in componenets)

{

int numbers = int.Parse(X);

for (int i = 0; i < binary.Length; i++)

{

results.Add(numbers * Math.Pow(2, i));

}

}

foreach (double Y in results)

{

Denary = Denary + Y;

}

Console.WriteLine(Denary);

Console.ReadLine();

@opeyemi

can you debug your code and find out the mistake you have done? because it would be better if you can find out the error by yourself. if the conversion logic is clear to you then you should locate the error by debugging.

Hello every one, I’ve been using python to deal with some simple problems, I am not a programmer and I am learning python for some time. This is my try in python:

ui = raw_input(‘Enter a binary number :’)

def b2d(s):

dec = 0

for i in range(1, len(s) + 1):

#iterate on s in reversed order

dec = dec + int(s[i*(-1)])*2**(i -1)

return dec

b2d(ui)

#include

using namespace std;

int calculatePower(int p)

{

int pw=1;

for(int i=1;i=1;i–)

{

sum=sum+bn[i]*calculatePower(i);

}

sum=sum+bn[0];

printf(“In decimal: %d”,sum);

return 0;

}

//binary to decimal convertion

#include

using namespace std;

int calculatePower(int p)

{

int pw=1;

for(int i=1;i=1;i–)

{

sum=sum+bn[i]*calculatePower(i);

}

sum=sum+bn[0];

printf(“In decimal: %d”,sum);

return 0;

}

@Hassan Nomani great program. way better than mine, but it would still return a value even if I enter let’s say 50 or 200.

I think you need to upgrade the code so that it doesnt return a value for non-binary inputs. do you know how that can be done?

@Hassan Nomani can you also explain this part?

i=0;

while(bin!=0)

{

j=bin%10;

sum=sum+j*pow(2,i);

bin=bin/10;

i++;

}

Hey Hassan, I’m new to the programming world, I have a doubt, you dint declare pow in your program. and I couldn’t find any functions like pow.. is it a default one..?