C program binary fraction to decimal conversion

This program converts a Binary Fraction to a Decimal Fraction. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1


#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
double ans,bin,frac,fun=0.0,a;
int sum=0,dec,i=0,rem,b,j=1;
printf("\nEnter the Binary number");
scanf("%lf",&bin);
dec=bin;
frac=bin-dec;
while(dec!=0)
{
rem=dec%10;
dec=dec/10;
sum=sum+(rem*pow(2,(i++)));
}
while(frac!=0.000)
{
a=10*frac;
b=a;
fun=fun+(b*(1/pow(2,(j++))));
frac=a-b;
}
ans=sum+fun;
printf("\nThe decimal equivalent is %lf",ans);
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment