How can I simplify a fraction

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
Describe to yourself how you would do it on paper.
Write code that implements that algorithm.
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
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?
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.
@blueberry

I believe 6/24 is actually simplified to 1/4, not 1/3
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
Topic archived. No new replies allowed.