Header Ads

13 - WAP to Print Fractional Decimal Number in Binary Number in C


This Program prints the Value of a Fractional Decimal to Binary Number.
#include 
//Main program function
int main()
{
 //Declaring the variables, 
 long double num, fractional_part, decimal = 0.0, factor1 = 0.1, result;
 long int rem, temp, idx, integer, binary = 0, factor = 1;
 printf ("Enter the Floating Point Number  : ");
 scanf ("%Lf", &num);
 integer = num;
 fractional_part = num - integer;
 //Checking for Integer not eq to 0.
 while (integer != 0)
 {
  rem = integer % 2;
  binary = binary + rem * factor;
  integer = integer / 2;
  factor = factor*10;
 }
 //getting the values of fractional part upto 6 places.
 for (idx = 1; idx < 6; idx++)
 {
  fractional_part = fractional_part * 2;
  temp = fractional_part;
  decimal = decimal + factor1 * temp;
  if (temp == 1)
   fractional_part = fractional_part - temp;
  factor1 = factor1 / 10;
 }
 //Fetching the binary number's and storing it in result
 result = binary + decimal;
 //Printing the result.
 printf ("The Binary Number is : %Lf \n",result);
 return 0;
}

Program Ouput:

 

No comments