# anyone help a newbie with fractions

• 07-24-2002
Unregistered
anyone help a newbie with fractions
Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.
• 07-25-2002
dbaryl
Well, first off: you need the algebra of it.

Whether you're trying to add or subtract, it's necessary to know the common denomenator. To do this, you can simply multiply top and bottom of each fraction by the denomenator of the other fraction. This will not be reduced, but we'll get to reducing later.

So to +/-

Say you have 2 fractions, numerator 1: n1, denomenator 1: d1, numerator 2: n2... and n3 & d3 for the final fraction. Assuming you have the values already stored (using a function like scanf):
Code:

```int n1, n2, n3, d1, d2, d3; n3 = n1 * d2 + n2 * d1; d3 = d1 * d2; //common denomenator, not reduced```
To subtract, just change the + to - in code above.

To multiply or divide:

To multiply, just multiply the numerators for new numerator, den. for new denomenator.
Code:

```int n1, n2, n3, d1, d2, d3; n3 = n1 * n2; d3 = d1 * d2; //common denomenator, not reduced```
To divide, just multiply by inverse.
Code:

```int n1, n2, n3, d1, d2, d3; n3 = n1 * d2; d3 = d1 * n2; //denomenator not reduced```
One thing that's left is to reduce... to do this, we can get the greatest common factor first, then divide top/bottom of the fraction by this value. You can find how to do this here:

If you have any other questions, just ask.
• 07-25-2002
face_master
Quote:

Originally posted by Unregistered
Can anyone help me I'm very new to programming and need some help. I need to find a way to +,-,*, and / fractions.
Gee, dbaryl, dont cofuse him.
Heres some code that adds one to a fraction (just fixed up that sentence). This fraction that we'll work with is
7
--
10
Code:

```// our fraction double fraction = 7 / 10; // remember, our fraction is equal to 7 divided by ten double total = 0; // our end result total = fraction + 1;```
Hopefully, I'm not tellin ya anything incorrect...just give that example a go.
• 07-25-2002
dbaryl
Man, you have confused ME now :D ...
Code:

`total = fraction + 1;`
what in the world is that? sorry, I don't get that.

Also, I was thinking they want it as a fraction, not a decimal, as that's what making it a double will do.
• 07-25-2002
face_master
its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?
• 07-25-2002
black
Algebra is basis, and next you should learn the difference between programming language and mathematics.

for instance, this line below is common in almost all kinds of programming languages but not in math.

PHP Code:

``` a=a+1;  ```

Good luck~ :)
• 07-25-2002
dbaryl
Quote:

Originally posted by face_master
its adding 1 to the fraction...um, theres no data structure that holds a fraction in any way other than a decimal, is there?
>>Heres some code that adds one to a fraction (just fixed up that sentence).

OK, I guess I need to pead more throughly next time :D