Consider below example
Convert 2.625 to binary.
We will consider the integer and fractional part separately.
The integral part is easy, 2 = 10.
For the fractional part:
0.625 ร 2 = 1.25 1 Generate 1 and continue with the rest.
0.25 ร 2 = 0.5 0 Generate 0 and continue.
0.5 ร 2 = 1.0 1 Generate 1 and nothing remains.
So 0.625 = 0.101, and 2.625 = 10.101.
See this link for more information.
Answer from Govind Prabhu on Stack Overflowfloating point - How to convert float number to Binary? - Stack Overflow
Converting decimal float values to binary form using C or C++
Convert From binary to Float in C
float to binary conversion
What is Float to Binary?
How do I convert data with Float to Binary?
Is Float to Binary free to use?
Videos
Consider below example
Convert 2.625 to binary.
We will consider the integer and fractional part separately.
The integral part is easy, 2 = 10.
For the fractional part:
0.625 ร 2 = 1.25 1 Generate 1 and continue with the rest.
0.25 ร 2 = 0.5 0 Generate 0 and continue.
0.5 ร 2 = 1.0 1 Generate 1 and nothing remains.
So 0.625 = 0.101, and 2.625 = 10.101.
See this link for more information.
Keep multiplying the number after decimal by 2 till it becomes 1.0:
0.25*2 = 0.50
0.50*2 = 1.00
and the result is in reverse order being .01
I've done writing the program which inputs integer decimal values and displays binary form of that input value in c/c++, but the original assignment was for float value inputs. So does anybody have any idea how to do that ?
This is the program that i've written :
#include <stdio.h>
int decimaltobinary(int decimal_number){
int binary[32];
int i =0;
while (decimal_number>0){
binary[i]=decimal_number%2;
decimal_number=decimal_number/2;
i++;
}
for (int k=i-1;k>=0;k--){
printf("%d",binary[k]);
}
return 0;
}
int main(void){
int number;
printf("Enter the number : ");
scanf("%d",&number);
decimaltobinary(number);
}