
C Programming Homework Help on Floats Binary to Decimal Conversion
- 13th May, 2022
- 15:21 PM
#include #include #include float b2f(char f_b[]){ // to store int and frac parts int a[20],b[20]; int i,j,k,cntint,cntfra,flag,rem; // variable for result float rem1; cntint=cntfra=flag=rem=0; rem1=0.0; // check for . and seperates binary for(i=0,j=0,k=0;i { if(f_b[i]=='.') { flag=1; } else if(flag==0){ a[j++]=f_b[i]-'0'; } else if(flag==1){ b[k++]=f_b[i]-'0'; } } cntint=j; cntfra=k; // creates integer part using array stored above for(j=0,i=cntint-1;j { rem = rem + (a[j]*pow(2,i)); } // creates fraction part using array stored above for(k=0,i=1;k { rem1 = rem1 + (b[k]/pow(2,i)); } //result which is sum of int and fraction rem1 = rem + rem1; return rem1; } int main() { char s[20]; printf("Enter the binary no : "); scanf("%s",s); printf("%f",b2f(s)); }