Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to convert decimal fraction to binary fraction
Consider the examples given below to understand how to convert a decimal fraction to binary fraction in C programming language.
Example 1 − Conversions of 25 to binary.
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
So equivalent binary number is: 11001
Example 2 − Conversions of 0.7 to binary.
Step 1 − 0.7 * 2 = 1.4, Int part = 1
Step 2 − 0.4 * 2 = 0.8, Int part = 0
Step 3 − 0.8 * 2 = 1.6, Int part = 1
Step 4 − 0.6 * 2 = 1.2, Int part = 1
Step 5 − 0.2 * 2 = 0.4, Int part = 0
Step 6 − 0.4 * 2 = 0.8, Int part = 0
So equivalent binary number is: 0.101100
Step 3 − Finally, the binary value of decimal number 25.7 is as follows −
11001 + 0.101100 = 1101.101100
Example
Following is the C program to convert 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);
dIntegral = fraDecimal;
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;
}
Output
When the above program is executed, it produces the following result −
Enter any fractional decimal number: 5.7 Equivalent binary value: 101.101100