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

Solution:

#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;
else
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;
}

### Like this:

Like Loading...

*Related*

## Published by Shahab

Completed B.Sc in CSE, @United International University, Dhaka.
Currently working as Development Engineer, Android and iOS Application.
View all posts by Shahab

I don’t have time for all the steps, so I am using an online converter like http://decimal2fraction.com I like it because it saves me a lot of time. Let me know what you think of it.

@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 🙂