convert an improper fraction to a mixed number

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to crete a method that will convert an improper fraction to a
mixed number...

I am not sure how about how to acomplish this. I know I can get the
remainder with the modulus operator (%), but I am not sure how to get the
quotent.

any insight would be appreciated.

thanks
 
Jeff said:
I am trying to crete a method that will convert an improper fraction to a
mixed number...

I am not sure how about how to acomplish this. I know I can get the
remainder with the modulus operator (%), but I am not sure how to get the
quotent.

any insight would be appreciated.

An example would be helpful. I think I know what you want but I am not
sure, as it seems quite simple to me.
 
int numerator;
int denominator;
....
int wholePart = numerator / denominator;
numerator = numerator % denominator;

you will now have a mixed number: whole part, numerator, and
denominator.
 
Incidentally, I built and entire Fraction class, with arithmetic
operations, normalization, etc, if it will help.
 
Thanks for the feedback. I would be very greateful if you would share your
fraction class and any knowledge about dealing with fractions. I am somewhat
of a novice programmer and could use advice.

I read an interesting articles on codeproject.com
(www.codeproject.com/csharp/Fractiion.asp)... however, this class does not
deal with mixed numbers and always reduces fractions to improper fractions,
if available.

thanks,
jeff reed
(e-mail address removed)
 
Example

improper fraction: 8/5

convert to mixed number...

divide the numerator (8) by denominator (5)... this gives a quotent of 1 and
remainder of 3... the remainder is placed over the divisor (5)

so... the answer is 1 3/5

However, using the arithmitic operators does not provide the answer of a
mixed number. Dividing 8/5 produces 1.6. Using the mod operator only provides
the remainder.

I am a novice programmer so that I am probably missing a simple proccess

Thanks
 
What types are you using to store your numerator and denominator? If
division yields 1.6 then you are perhaps using decimal, float, or
double? There is no need to use these for fractions. (You would never
want to have a fraction of 1.6 / 5, for example.)

Use int or long for the types of your numerator and denominator.
Division will then truncate, so 8 / 5 will give you 1, not 1.6.
 
Back
Top