Convert Decimal into Irreducible Fraction

Objective: Given a decimal number, convert it into irreducible fraction.

Irreducible Fraction : An irreducible fraction is a fraction in which the numerator and denominator are integers that have no other common divisors 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 decimal length
  • Calculate the denominator
  • Calculate the numerator Ex 1.2*10 = 12 { (int) Math.pow(10, b)}
  • Find the greatest common divisor between numerator and denominator.
  • Now irreducible fraction = “” + numerator / gcd + “/” + denominator / gcd.

Code:


Output:

7/20
6/5

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

%d bloggers like this: