Question: I am writing a program in C that will convert a floating point number to a base two binary number. It then normalizes it. so

I am writing a program in C that will convert a floating point number to a base two binary number. It then normalizes it. so 3.74 outputs 1.111E1 . I am not supposed to have trailing zeros in the binary representation. so It will only print out until the last one on the left hand side. so 140.1 will print 1.0001100000110011001101E7. Can someone help me with this? Thanks

#include

int main()

{

int integral, binaryInt = 0, i = 1;

float binaryFract = 0, k =0.1f, fractional, temp, binaryTotal, f;

printf("Convert float to binary ");

printf(" Enter float value : ");

scanf("%f",&f);

integral = (int)f;

//Separating the fractional part of the value

fractional = f - (int)f;

//First lets convert integral part to Binary

while(integral>0)

{

binaryInt = binaryInt + integral % 2 * i;

i = i * 10;

integral = integral / 2;

}

//Loop for converting Fractional value to binary now

while(k>0.00000001)

{

temp = fractional *2;

binaryFract = binaryFract+((int)temp)*k;

fractional = temp - (int)temp;

k = k / 10;

}

//Combining the integral and fractional binary value.

binaryTotal = binaryInt + binaryFract;

printf(" binary equivalent = %lf ", binaryTotal);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!