Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to convert decimal fraction to binary fraction
Converting decimal fractions to binary involves two separate processes: converting the integer part and the fractional part. The integer part uses division by 2, while the fractional part uses multiplication by 2.
Syntax
// For integer part: repeatedly divide by 2, collect remainders // For fractional part: repeatedly multiply by 2, collect integer parts
Algorithm
For Integer Part (25) −
- Step 1 − 25 / 2 Rem: 1, Quo: 12
- Step 2 − 12 / 2 Rem: 0, Quo: 6
- Step 3 − 6 / 2 Rem: 0, Quo: 3
- Step 4 − 3 / 2 Rem: 1, Quo: 1
- Step 5 − 1 / 2 Rem: 1, Quo: 0
Reading remainders from bottom to top: 11001
For Fractional Part (0.7) −
- Step 1 − 0.7 × 2 = 1.4, Integer part = 1
- Step 2 − 0.4 × 2 = 0.8, Integer part = 0
- Step 3 − 0.8 × 2 = 1.6, Integer part = 1
- Step 4 − 0.6 × 2 = 1.2, Integer part = 1
- Step 5 − 0.2 × 2 = 0.4, Integer part = 0
- Step 6 − 0.4 × 2 = 0.8, Integer part = 0
Reading integer parts from top to bottom: 0.101100
Final result: 25.7 decimal = 11001.101100 binary
Example
The following C program converts a decimal fraction to binary fraction −
#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: ");
scanf("%Lf", &fraDecimal);
/* Separate integer and fractional parts */
dIntegral = fraDecimal;
dFractional = fraDecimal - dIntegral;
/* Convert integer part to binary */
while (dIntegral != 0) {
remainder = dIntegral % 2;
bIntegral = bIntegral + remainder * intFactor;
dIntegral = dIntegral / 2;
intFactor = intFactor * 10;
}
/* Convert fractional part to binary (up to 6 decimal places) */
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<br>", fraBinary);
return 0;
}
Enter any fractional decimal number: 5.7 Equivalent binary value: 101.101100
Key Points
- Integer conversion uses division by 2 method with remainders collected in reverse order.
- Fractional conversion uses multiplication by 2 method with integer parts collected in forward order.
- Some decimal fractions cannot be exactly represented in binary (like 0.7), leading to repeating patterns.
- The program limits fractional precision to 6 decimal places to avoid infinite loops.
Conclusion
Converting decimal fractions to binary requires separate handling of integer and fractional parts. While integer conversion is straightforward, fractional conversion may result in approximations due to binary representation limitations.
