/****************************************************/ /* DECIMAL-TO-BINARY FRACTION CONVERTER */ /* Source code written by: A. Yavuz Oruç */ /* E-mail: yavuz@eng.umd.edu */ /* Last revised: September 1999 */ /* Input: A decimal fraction */ /* Output: A binary fraction */ /* */ /****************************************************/ // AYO: This program converts decimal fractions to binary fractions. // The decimal fraction should be entered with a decimal point followed // by a sequence of decimal digits. For example, .223, .0123, .345. // Entries such as 0.1234 will not work. // The number of digits in the binary fraction is capped at 50. // If you want to increase it, just change the argument of B. #include <stdio.h> main() { int i,j; short B[50]; float n; while(true) { for (i=0; i<50; i++) B[i] = 0; i = 0; //Here enter the decimal fraction printf("Enter decimal fraction starting with a decimal point: "); scanf("%f",&n); //Here convert the decimal fraction to binary fraction while (n != 0 && i < 50) { n = 2.0*n; if (n < 1.0) { B[i] = 0;} else { B[i] = 1; n = n - 1.0; } i = i + 1; } //Here print the binary fraction printf("Binary fraction is: "); for (j=0; j<i; j++) printf("%d",B[j]); printf("\n"); } } /* Sample Run Enter decimal fraction starting with a decimal point: .125 Binary fraction is: 001 Enter decimal fraction starting with a decimal point: .25000 Binary fraction is: 01 Enter decimal fraction starting with a decimal point: .0250 Binary fraction is: 000001100110011001100110011 Enter decimal fraction starting with a decimal point: .750 Binary fraction is: 11 Enter decimal fraction starting with a decimal point: .875 Binary fraction is: 111 Enter decimal fraction starting with a decimal point: .999999 Binary fraction is: 111111111111111111101111 Enter decimal fraction starting with a decimal point: .9999999999999 Binary fraction is: 111111111111111111111111 Enter decimal fraction starting with a decimal point: .99999999999999999 Binary fraction is: 11111111111111111111111111111111111111111111111111 Enter decimal fraction starting with a decimal point: .555555555 Binary fraction is: 100011100011100011100011 Enter decimal fraction starting with a decimal point: */