top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Convert a binary number into decimal number in c?

+2 votes
815 views
Convert a binary number into decimal number in c?
posted May 30, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Here is simple and elegant solution to convert a number from binary to decimal. In this code i have taken binary value as string and then applied bruteforce method to convert it to decimal,here i have used character array that is why while evaluating decimal value i have subtracted 48 which is ASCII value of 0.

#include <stdio.h>
#include<string.h>
#include<math.h>

int BinaryToDecimal(char *p){
    int lenStr,i,j=0,decimalNum=0;
    lenStr=strlen(p);
    for(i=lenStr-1;i>=0;i--){
        decimalNum+=((p[i]-48)*pow(2,j));
        j++;
    }
  return decimalNum;
}
int main(void) {
    char Binary[10000];
    printf("enter a binary number:");
    scanf("%s",Binary);
    printf("%d\n",BinaryToDecimal(Binary));
    return 0;
}
answer May 31, 2016 by Shahsikant Dwivedi
0 votes

Source Code to Convert Either Binary Number to Decimal or Decimal Number to Binary

/* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */

#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
   int n;
   char c;
   printf("Instructions:\n");
   printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
   printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
   scanf("%c",&c);
   if (c =='d' || c == 'D')
   {
       printf("Enter a binary number: ");
       scanf("%d", &n);
       printf("%d in binary = %d in decimal", n, binary_decimal(n));
   }
   if (c =='b' || c == 'B')
   {
       printf("Enter a decimal number: ");
       scanf("%d", &n);
       printf("%d in decimal = %d in binary", n, decimal_binary(n));
   }
   return 0;
}

int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{
    int rem, i=1, binary=0;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/

{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(2,i);
        ++i;
    }
    return decimal;
}

Output

Instructions:

1. Enter alphabet 'd' to convert binary to decimal.
2. Enter alphabet 'b' to convert decimal to binary.
d
Enter a binary number: 110111
110111 in binary = 55 in decimal

This program asks user to enter alphabet 'b' to convert decimal number to binary and alphabet 'd' to convert binary number to decimal. In accordance with the character entered, user is asked to enter either binary value to convert to decimal or decimal value to convert to binary.

To perform conversion, two functions are made decimal_binary(); to convert decimal to binary and binary_decimal(); to convert binary to decimal. Decimal number entered by user is passed to decimal_binary() and this function computes the binary value of that number and returns it main() function. Similarly, binary number is passed to function binary_decimal() and this function computes decimal value of that number and returns it to main() function.

answer May 30, 2016 by Manikandan J
Hi Manikandan, Can we convert this without use of / and % operators?if yes please share cpp code.
Similar Questions
+3 votes

Decimal to Factorial
23(D) = 3210(F)

23 / 3! = 3 (5)
5 / 2! = 2 (1)
1 / 1! = 1 (0)
0 / 0! = 0 (0)

Factorial to Decimal
3210(F)=23(D),
3*3!+2*2!+1*1!+0*0! = 23(D)

+4 votes

How to convert a binary tree into binary search tree keeping the structure of tree intact?

...