Write a c program to convert binary number to decimal number






C code for binary to decimal conversion:



#include<stdio.h>

int main(){
   
    long int binaryNumber,decimalNumber=0,j=1,remainder;

    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);

    while(binaryNumber!=0){
         remainder=binaryNumber%10;
        decimalNumber=decimalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
    }

    printf("Equivalent decimal value: %ld",decimalNumber);

    return 0;
}

Sample output:

Enter any number any binary number: 1101
Equivalent decimal value: 13


Algorithm:

Binary number system: It is base 2 number system which uses the digits from 0 and 1.

Decimal number system:
It is base 10 number system which uses the digits from 0 to 9


Convert from binary to decimal algorithm:



For this we multiply each digit separately from right side by 1, 2, 4, 8, 16 … respectively then add them.



Binary number to decimal conversion with example:

For example we want to convert binary number 101111 to decimal:
Step1:  1 * 1 = 1
Step2:  1 * 2 = 2
Step3:  1 * 4 = 4
Step4:  1 * 8 = 8
Step5:  0 * 16 = 0
Step6:  1 * 32 = 32

Its decimal value: 1 + 2 + 4+ 8+ 0+ 32 = 47

That is (101111)2 = (47)10






6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

20 comments:

Anonymous said...

ok now the question is , what if I post 123 , or 2985 , in base of 2. Eventually, This isn't binary (base of 2 , combination os 1s and 0s). what code to use to make the program know its not a binary code ?

sridhar M N said...

I can't understand this program please explain with step

Bharat Lohakare said...

A very simple and easy steps to understand please keep on posting such new concept. Also try to explain single example with different types of logic if available.

Anonymous said...

what does the "j" stand for?

Unknown said...

j is used as "base"
initially it is 1.
After first iteration it will be 1*2=2
After second iteration it will be 2*2=4 and so on..

Anonymous said...

#include
int main()
{
int num, x = 0, y = 0, z = 1;

printf("Enter Your Number: ");
scanf("%i", &num);

while(num > 0)
{
x = num % 10;
y = y + (x * z);
z = z * 2;
num = num / 10;
}

printf("%i", y);

getchar();
return 0;
}

Anonymous said...

#include
int main()
{
int num = 1101, x = 0, y = 0, z = 1;

//printf("Enter Your Number: ");
//scanf("%i", &num);

while(num > 0)
{
x = num % 10;
y = y + (x * z);
z = z * 2;
num = num / 10;
}

printf("%i", y);

getchar();
return 0;
}

Anonymous said...

THANKS FOR HELP US.

Shamim Ahmed said...

#include
#include

int main()
{
int dec=0,bin,rem,i=0;
printf("Enter the binary number (0,1): ");
scanf("%d",&bin);
while(bin!=0)
{
rem=bin%10;
dec+=rem*(pow(2,i));
i++;
bin=bin/10;
}
printf("\nDecimal value is= %d\n",dec);
return 0;
}

Unknown said...
This comment has been removed by the author.
Anonymous said...

Time Limit Exceeded. how to improve this algorithm

Pavithra said...

how do i go about with binary numbers consisting of decimals? example: 1011.11

Anonymous said...

Good. Thank you

Unknown said...

how to limit to only 5 bits. for example ( it i enter more than 5 bits of binary number it will show me "do it again", and if it is lees or equal to 5 bits will work).

thanks

Unknown said...

Who on earth would do that if it is clearly mentioned to input binary?

Unknown said...

Use bitwise operators!

Unknown said...

When I enter bin value = 10 000 000 000, it'll show decimal value = 1824 which is wrong.
I don't understand.

Allen jeley said...

cool

Unknown said...

Binary to decimal conversion:
#include
#include
int main()
{
int d,i,n[100],r,b=0,len,j;
printf("what is the bit limit of your binary value:");
scanf("%d",&r);
printf("\n the value of binary number is:\n");
for(i=0;i<r;i++)
{
scanf("%d",&n[i]);
}
len=i;
d=len-1;
for(i=0;i<len;i++)
{
b+=n[i]*pow(2,d);
d--;
}
printf("the decimel is:%d",b);
getch();
return 0;
}
input:
what is the bit limit of your binary value:5
the value of binary number is:
1
0
1
0
1
output: the decimal is:21

Unknown said...

Decimal to Binary conversion:
#include
#include
int main()
{
int d,i,n[100],r=0,b=0,len,j;
printf("the value of decimal number is:");
scanf("%d",&d);
b=d;
for(i=0;i=0;j--)
{
printf("%d",n[j]);
}

getch();
return 0;
}
input:
the value of decimal number is :21
the binary of 21 is :10101