Decimal To Fraction Conversion

General Statement: Convert a number in decimal form to the equivalent mixed number with the fraction portion reduced to lowest terms.

Input: There are an unknown number of values on a single line. A –1 is used to indicate the end of the data set.

Output: Output the integer part of the number, followed by the word AND where the decimal point was, and then the reduced fraction. There is to be a division symbol (/) between the numerator and the denominator of the fraction.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: The numbers are in the range 1..500. There are no repeating decimals.
The –1 used to indicate the end of the data set is not part of the data for the problem.

Sample Input:
2.95 14.2 5.625 -1

Sample Output:
2 AND 19/20
14 AND 1/5
5 AND 5/8


#include <stdio.h>

int main ()
    double input;

    while ( scanf ("%lf", &input) & input != -1 ) {
        int system = input;
        input -= system;

        int isave;
        int jsave;
        double temp;
        double min = 1;

        for ( double i = 1; i < 65; i++ ) {
            for ( double j = i + 1; j < 65; j++ ) {

                if ( i / j > input )
                    temp = i / j - input;
                    temp = input - i / j;

                if ( temp < min ) {
                    isave = (int) i;
                    jsave = (int) j;
                    min = temp;

        printf ("%d AND %d/%d\n", system, isave, jsave);

    return 0;

2 thoughts on “Decimal To Fraction Conversion

  1. @Alex
    unfortunately, this post title matches with your purpose. which is not the case. i am afraid, you are in a wrong place, the whole blog is basically for programmers/developers, not for users.
    you can easily convert using your mentioned tool. we are the people who make these tools and write the behind logic. Thanks for your concern🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s