
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
- Related Articles
- Convert decimal fraction to binary number in C++
- How to convert a decimal into a fraction and a fraction into a decimal?
- Fraction to Recurring Decimal in C++
- C# Program to Convert Binary to Decimal
- Convert the following decimal into fraction: 3.72
- How To Convert Mixed fraction into Improper Fraction
- C# Program to Convert Decimal to Binary\n
- C++ Program To Convert Decimal Number to Binary
- Convert the following percentage into fraction and decimal.
- How to convert the mixed fraction into an improper fraction?
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- How to convert a decimal number to a fraction reduced to its lowest form?
