## INDEX

### C program for fractional decimal to binary fraction conversion

C code for fractional decimal to binary converter:

#include
<stdio.h>

int main(){

long double fraDecimal,fraBinary,bFractional = 0.0,dFractional,fraFactor=0.1;
long int dIntegral,bIntegral=0;
long int intFactor=1,remainder,temp,i;

printf("Enter any fractional decimal number: ");

dFractional =  fraDecimal - dIntegral;

while(dIntegral!=0){
remainder=dIntegral%2;
bIntegral=bIntegral+remainder*intFactor;
dIntegral=dIntegral/2;
intFactor=intFactor*10;
}

for(i=1;i<=6;i++){

dFractional = dFractional * 2;
temp =  dFractional;

bFractional = bFractional + fraFactor* temp;
if(temp ==1)
dFractional = dFractional - temp;

fraFactor=fraFactor/10;
}

fraBinary =  bIntegral +  bFractional;
printf("Equivalent binary value: %lf",fraBinary);

return 0;
}

Sample output:

Enter any fractional decimal number: 5.7
Equivalent binary value: 101.101100

Algorithm:

How to convert fractional decimal to binary:

Following steps describe how to convert decimal to binary

Step1. First we convert the integral part of binary number to decimal.

Following steps describe how to convert decimal number to binary number:

S1: Divide the decimal number by 2
S2: Divide the quotient by 2
S3: Repeat the step 2 until we get quotient equal to zero.

Equivalent binary number would be remainders of each step in the reverse order.

Step2. Now we convert the fractional part of decimal number to binary.
Following steps describe how to convert floating decimal to binary

S1: Multiply the decimal number by 2
S2: Integral part of resultant decimal number will be first digit of fraction binary number.
S3: Repeat the S1 using only fractional part of decimal number and then S2.

Step3: Add the integral and fractional part of binary number.

Example for floating point decimal to binary:
For example we want to convert the decimal number 25.7 to binary number.

Step1: Conversions of 25 to binary.

S1:  25 / 2  Remainder : 1 , Quotient : 12
S2:  12 / 2  Remainder : 0 , Quotient : 6
S3:   6 / 2  Remainder : 0 , Quotient : 3
S4:   3 / 2  Remainder : 1 , Quotient : 1
S5:   1 / 2  Remainder : 1 , Quotient : 0

So equivalent binary number is: 11001

Step2: Conversions of .7 to binary.

S1: 0.7 * 2 = 1.4, Integral part = 1
S2: 0.4 * 2 = 0.8, Integral part = 0
S3: 0.8 * 2 = 1.6, Integral part = 1
S4: 0.6 * 2 = 1.2, Integral part = 1
S5: 0.2 * 2 = 0.4, Integral part = 0
S6: 0.4 * 2 = 0.8, Integral part = 0

So equivalent binary number is: 0.101100

Step 3: So binary value of decimal number 25.7 will be
11001 + 0.101100 = 1101.101100

6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

Unknown said...

returning value is -0.000000 irrespective of any no. Anonymous said...

This has been fantastically helpful! Thank you so much. Anonymous said...

It is a wrong program, output always -0.000000000 Anonymous said...

WRONG..!!!
Plz provide correct code.
Thanks..

Unknown said...

In 39th line replace %lf by %Lf..
will work..
Good work man..!! (Y)

Unknown said...

Doesn't work :/ But thanks for the idea :)

Unknown said...

I´ve just did some minor tweaks and it started working just great.

1.) Included all standard libraries, so I started with:

#include
#include
#include

2.) Changed long integers and floats for the regular ones.

3. Renamed fraDecimal to just f, because I usually do.

I am not sure which one helped, but works flawlessly now, even on C, not even C++.