Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Convert Decimal into Irreducible Fraction

Objec­tive: Given a dec­i­mal num­ber, con­vert it into irre­ducible fraction.

Irre­ducible Frac­tion : An irre­ducible frac­tion is a frac­tion in which the numer­a­tor and denom­i­na­tor are inte­gers that have no other com­mon divi­sors than 1. Ex: 1/4, 5/20, 1/2 etc

Example:

Input: 0.35
Output : 7/20

Input: 1.2
Output : 6/5

Approach:

  • Split using decimal
  • Find the dec­i­mal length
  • Cal­cu­late the denominator
  • Cal­cu­late the numer­a­tor Ex 1.2*10 = 12 { (int) Math.pow(10, b)}
  • Find the great­est com­mon divi­sor between numer­a­tor and denominator.
  • Now irre­ducible frac­tion = “” + numer­a­tor / gcd + “/” + denom­i­na­tor / gcd.

Code:


Out­put:

7/20
6/5

You may also like...