![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
I'd say an easier way of doing this would be to just divide both numbers by their GCF. Finding the GCF will proabably be the hardest part.
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 3
Rep Power: 0
![]() |
Thanks for the response, I'll see what I can do about that and post back.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Sep 2005
Posts: 3
Rep Power: 0
![]() |
Thanks a lot for that info. I figured it out. Thanks for all the help guys.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|