Thursday, May 23, 2019

To convert Binary to Decimal

Write a C Program to Convert Binary to Decimal Number System

Program:-

#include <stdio.h>
#include <math.h>
int binaryToDecimal(long binarynum)
{
    int decimalnum = 0, temp = 0, remainder;
    while (binarynum!=0)
    {
        remainder = binarynum % 10;
        binarynum = binarynum / 10;
        decimalnum = decimalnum + remainder*pow(2,temp);
        temp++;
    }
    return decimalnum;
}

int main()
{
    long binarynum;
    printf("Enter a Binary Number: ");
    scanf("%ld", &binarynum);

    printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum));
    return 0;
}

Output:-

Enter a Binary Number: 110011
Equivalent decimal number is: 51
--------------------------------
Process exited after 20.4 seconds with return value 0
Press any key to continue . . .


0 Comments:

Post a Comment