C program that takes as input a positive fractional number and converts it to binary
Posted by Samath
Last Updated: January 01, 2017

Write a C program that takes as input a positive fractional number and converts it to binary.

Code:

```#include<stdio.h>
#include<stdlib.h>
main (void) //Program Start
{
float Num; //Variables initialization. In this code "float" is used fot the
float Denom; //fraction division and "int" is used for the other variables and
int i; //parameters.
int Number1;
int n=0;
int Numb[n];
int j;
int x=0;
printf("Please enter the positive fraction numerator:\n");
scanf("%f", &Num); //Asking for the numerator for the fraction division
printf("Please enter the positive fraction denominator:\n");
scanf("%f", &Denom); //Asking for the denominator for the fraction division
Num = Num/Denom; //Numerator and denominatior division
i = Num; //The division result is assigned to the "i" variable
Number1 = i; //The value of "i" is assigned to the variable "Number1"
Denom = Num-i; //The decimal side of the number is obtained
do
{
Numb[n]= Number1%2; //Division remainder (residue)
Number1 = Number1/2; //New value divided by 2
n++; //The value of "n" is increasing during the "do-while" is running
}
while(Number1 != 0 ); //Comparing the number with "0"

printf("\n");
for(i=n-1; i>=0; i--) //Reversing the numbers order
{
printf("%d", Numb[i]);
}
printf("."); //Point between the integer and decimal part of the number
Num = Denom; //Decimal value is asigneg to the "Demon" variable
do
{
Num = Num*2; //Multiplpy by 2 for reached the 15 values
j=Num; //Comparation
if(j>0) //If the "j" value is greater a "1" is printed because a
{	 //mutliplication by 2 give a result with a integer part and
printf ("1"); //with a decimal part.
Num = Num-j;
}
else //If the result of the multiplication dont give a integer
printf ("0"); //part or a decimal part a "0" is printed.
x++;

}
while(x!=15); //The maximun quantity of values for a binary number is 15
printf("\n");
system ("PAUSE"); //Pausing the result screen after the program run
} //Program End```

How do you use the BINARY_CHECKSUM function to generate a hash value for a binary data?
PaulAnd | Jun 21, 2024
How do you use the BINARY data type to store binary data?
MaryJns | Jul 11, 2024
C program that determines if a number is either positive, zero, or negative
Samath | Nov 07, 2014
Java program that prints the sum of the first ten positive integers
Samath | Jan 08, 2021
Java program that prints the product of the first ten positive integers
Samath | Jan 08, 2021
Java program that reads an integer and prints if it's positive or negative
Samath | Feb 11, 2021
Java program that reads a floating ­point number and prints zero if the number is zero. Otherwise, print positive or negative
Samath | Feb 11, 2021
Decimal to Binary in C++
Samath | Dec 26, 2014
Python Convert Decimal to Binary or Binary to Decimal
Samath | Jan 09, 2017
C program that receives a positive float number and divides it by two until the result is less than or equal to zero
Samath | Jun 12, 2015
C program that prompts the user for a positive integer and then computes the sum of all the digits of the number
Samath | May 04, 2016
Binary Search Tree in C++
Samath | Jan 07, 2021
How to add and retrieve record in a binary file using C++
Samath | Jan 06, 2021
C program that takes a non- negative integer number as input and prints its binary representation
Samath | Jan 01, 2017