SCE

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