How can I simplify a fraction

May 6, 2013 at 11:19pm
For example if you enter 6/8 the program will display 3/4. My teacher said it has something to do with the common factor but im not coming up with anything.

Last edited on May 6, 2013 at 11:20pm
May 6, 2013 at 11:24pm
Describe to yourself how you would do it on paper.
Write code that implements that algorithm.
May 6, 2013 at 11:27pm
To simplify a fraction, you divide both numbers in the fraction by the greatest common factor(GCF).

The GCF is the biggest number that you can evenly divide both numbers by.

For example:

6 / 24 = 1 / 3

6 and 24 are dividable by 1, but this is not the GCF.

6 and 24 are dividable by 2, but this is not the GCF.

6 and 24 are dividable by 3, but this is not the GCF.

24 is divisible by 4 but 6 is not, so this is not a common factor.

Neither is divisible by 5.

Both are divisible by 6, and there is nothing higher that both numbers are divisible by. Therefore, 6 is the GCF

Now we divide 6 by 6: 6 / 6 = 1
And divide 24 by 6: 24 / 6 = 3

Therefore:
6 / 24 = 1 / 3

-Blueberry
May 6, 2013 at 11:28pm
I have. I would say ok well since both 6 and 8 are divisible by 2, ill divide the num and dem by 2. Ok its 3/4. But using that method, id have to make it so it would cycle through 2, 3, 4, 5, 6, 7, 8, 9. Isnt there a simpler way?
May 6, 2013 at 11:44pm
But using that method, id have to make it so it would cycle through 2, 3, 4, 5, 6, 7, 8, 9. Isnt there a simpler way?


There are certainly other ways to do it, and you can easily find these ways by learning to google. On the other hand, the algorithm you describe is very simple to implement. So, either research another algorithm or get to work on this one. Either way, feel free to ask questions when you have actual code you're trying to write.
May 11, 2013 at 4:46pm
@blueberry

I believe 6/24 is actually simplified to 1/4, not 1/3
May 11, 2013 at 5:42pm
use the modulo or how ever you spell that thing lol ( % ) then use a loop maybe something like
1
2
3
4
5
6
7
8
9
unsigned int gcf = 1;
for( unsigned int i = 2; i < HIGHESTNUMBER + 1; i++ ) //highest + 1 in case for 
//some reason the highest is the same for both eg 1/1 or 4/4
{
     if( denominator % i == 0 && numerator % i == 0)
     {
           gcf = i;
      }
}

haven't tested but I think it should work
Last edited on May 11, 2013 at 5:44pm
Topic archived. No new replies allowed.