write a program to convert binary number to decimal number in c language | CTechnotips


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


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

0 comments:

Post a Comment