#include<stdio.h> #define MAX 1000 int main(){ long double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5; long int dIntegral = 0,bIntegral=0,bFractional[MAX]; long int intFactor=1,remainder,i=0,k=0,flag=0; char fraBinary[MAX]; printf("Enter any fractional binary number: "); scanf("%s",&fraBinary); while(fraBinary[i]){ if(fraBinary[i] == '.') flag = 1; else if(flag==0) bIntegral = bIntegral * 10 + (fraBinary[i] -48); else bFractional[k++] = fraBinary[i] -48; i++; } while(bIntegral!=0){ remainder=bIntegral%10; dIntegral= dIntegral+remainder*intFactor; intFactor=intFactor*2; bIntegral=bIntegral/10; } for(i=0;i<k;i++){ dFractional = dFractional + bFractional[i] * fraFactor; fraFactor = fraFactor / 2; } fraDecimal = dIntegral + dFractional ; printf("Equivalent decimal value: %Lf",fraDecimal); return 0; }
Filled Under
C
C code for fractional binary to decimal converter
Related:

C code for binary to decimal conversion

C code for how to convert large binary to octal

C program to convert binary to octal

C program for hexadecimal to binary conversion

C program to change octal to decimal

C code to convert octal number to decimal number

Convert numbers to roman numerals in c

C code for product of two binary numbers
Subscribe to:
Post Comments (Atom)