| wik865 |
Sep 21st, 2005 9:32 PM |
Java fraction help
I have a program due that requires me to deal with adding, multiplying, dividing, subtracting, and simplifying fractions. I'm stuck on the simplifying part. This is a generic coding for the simplification part:
:
class Reduce
{
int den, num, i = 2, cd = 0;
Reduce(int n, int d)
{
num = n;
den = d;
}
void reduction()
{
do
{
if ((den % i == 0) && (num % i == 0))
cd = i;
else i++;
}
while (cd == 0);
System.out.println(cd);
num /= cd;
den /= cd;
System.out.println(num + "/" + den);
}
}
class Test
{
static public void main(String[] a)
{
Reduce r1 = new Reduce(3,9);
r1.reduction();
}
}
It seems to work with fractions like 3/9 and 2/6, fractions that are able to be simplified. If I try with say 3/8, the output is 1/4. I was wondering if anyone out there can help me figure out why it is doing that. It's frustrating me a lot and I would like to finish this program soon.
|