-
How to make this work =(
The question is to input a non-negative real number:
and approximate the closest fraction.
so i.e.
if
Nonnegative real number: 0.751
Closest approximation = 3/4
( 42.7488 -> approx. 171/4 ...and so on)
Code:
#include <stdio.h>
int main() {
double p, pre, cur;
int i, j, x, y, d;
printf("Nonnegative real number: ");
scanf("%d.%p", &d, &p);
while(cur>pre){
for (i=1; i<=100; i++){
for (j=1; j<99; j++){
pre = (double)j/i - p;
}
}
for (x=2; x<=100; x++){
for(y=2; y<99; y++){
cur = (double)y/x - p;
}
}
if (cur > pre)
break;
break;
}
printf("Closest approximation = %d/%d, ", (x*d)+y, x);
return 0;
}
-
I would say the closest fraction for 0.751 is: 751/1000